I have a nested form in a Rails 3.1 app, using Cocoon gem.
In the child form, I have a link that, when clicked, should return the id of the child and pass it to a javascript function.
<%= form_for @parent do |f| %>
parent fields...
<%= f.fields_for :child do |child| %>
<%= link "run function", 'string-to-pass' => "#{child.id}" %>
child fields...
<% end %>
<% end %>
What is the correct syntax to return a column variable from a nested record?
child.id returns undefined local variable or method "child"
I could define an @child variable in the parent controller, but how do I point it towards the child ID, rather than the parent ID?
I’m sure I’m missing something simple here, but I can’t see how this fits together?
Grateful for any suggestions or pointers to relevant information.
Thanks!
I would suggest doing something like:
child.object.idI haven’t tested it so please let me know if it works.
EDIT:
I have code like this in my view:
And then in
_child_fields.html.erb:And this works.
Can you try adapting your code to be similar. One thing I just realized, in your fields_for you need to say:
rather than:
Assuming, of course that you have a one-to-many relation going on here.