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?

Re: Using Ajax to pull partial form from nested model

You are defaulting to the same as posted below,  so this probably  isn't much help,  but just to make it more obvious

<%= render :partial => "question_fields", :locals => { :f => builder } %>

I remember struggling with that back with Rails 2.X,  and I could not get around it,  but things have changed so much with regards to AJAX in 3.X (which I haven't done much with),  that there is likely an alternate approach to achieve what you want. 

In 2.X,  there was no way I could find to re-create the context of the f so the partial would render in an AJAX response.

Maybe there is now in 3.X,  punt......

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.