When using nested attributes mass assignment sometimes you will want to add
new records with javascript. You can do it with pure javascript, but if HTML is long your javascript will be long and messy
and it will not be DRY as probably you already have a partial for it.

So to add a partial dynamically you can do something like that (notice
string “index_to_replace_with_js”):

At the first sight it looks like it’s working because something =
“blahblahblah” is valid. However, so is this: something = “blah meh
55”. It’s just that your regex matched a substring of the value and not
the whole thing. The proper regex you’re looking for is actually:

The code posted by @hosiawak will still work in recent versions of Rails, but maybe a more current, idiomatic way to
do it is to stick this inside the Rails::Initializer block in
environment.rb (obviously you’ll also need to restart your server to pick
up the config change):