Reputation: 12663
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!
Upvotes: 1
Views: 277
Reputation: 2967
I would suggest doing something like: child.object.id
I haven't tested it so please let me know if it works.
EDIT: I have code like this in my view:
<%= f.fields_for :children do |child| %>
<%= render "child_fields", :f => child %>
<% end %>
And then in _child_fields.html.erb
:
<%= f.object.some_attribute %>
and other view stuff
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:
<%= f.fields_for :children
rather than:
<%= f.fields_for :child
Assuming, of course that you have a one-to-many relation going on here.
Upvotes: 1
Reputation: 5227
Don't really understand why you're talking about @variables in the controller if you want to call a javascript function, but anyway... I think you're looking for the helper link_to_function with some code like:
link_to_function('link', "myJSFunction(#{child.id});"
Is that your problem?
Upvotes: 0