Topic: Using Ajax to pull partial form from nested model
Hi all, I've got a question I'm sure is simple but I just cant figure out!
I was following the "Nested Model Form" on Asciicasts and was wondering if theres any way to dynamically insert the new form using true Ajax straight from a controller's partial, instead of via a helper which embeds javascript at run-time?
I'm using the following ajax link to submit the request:
<%= link_to "Add new question", survey_question_path(2, 'new'), :remote => false %>(1) how do I get the dynamic survey_id instead of hardcoding the id 2? (sorry quite new to Rails here)
(2) more importantly, how can I pass the question Form Builder to the partial view (_question_fields.html.erb)??
I hope this isnt too cryptic, here are the code bits to make things clearer:
==> the main form (survey/_form.html.erb)
<% form_for @survey do |f| %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<% f.fields_for :questions do |builder| %>
<%= render 'question_fields', :f => builder %>
<% end %>
<p><%= f.submit "Submit" %></p>
<% end %> ==> and the partial view (_question_fields.html.erb)
<p>
<%= f.label :name, "Display name" %><br />
<%= f.text_field :name %>
</p>but when _question_fields.html.erb is rendered from an ajax call, it doesnt know what "f" is anymore! How can my controller pass the "f" to the partial view?
Hope someone with more Rails experience than me can help,cos I've spent an entire day trying to solve this and its not getting anywhere. Hasnt anyone tried to do the same thing?