Passing form object when using Ajax in rails

I have a complex survey form. Overview

belongs_to :template
has_many :questions, :through => :template
has_many :answers, :through => :questions

      

those. The user creates a new survey and must select a survey template. The survey template will create some default questions and answer fields.

<%= form_for @survey do |f| %>

  <p>Select a template:</p>
  <%= render @templates, :locals => {:patient => @patient }, :f => f %>

<% end %>

      

Then I render the partial _templates

and I still have access to the form object. From here the idea is that the user can click on the template name and the template questions and answers will be passed through Ajax.

<% @templates.each do |template| %>

  <div class="thumbnail">
    <%= link_to template.name,
                { :controller => "surveys",
                  :action => "new",
                  :template_id => template.id,
                  :patient_id => nil,
                  :f => f,
                  :remote=> true },
                :class=> "template", :id=> template.id %>
  </div>

<% end %>

      

Survey # new:

respond_to :js, :html

def new
  @template = Template.find(params[:template_id])
  @patient = Patient.find(params[:patient_id])
end

      

new.js.erb:

$('#assessment').append("<%= j (render new_survey_path, :locals => {:f => f} ) %>");

      

new_survey_path:

<%= f.fields_for :questions do |builder| %>
  <% @template.questions.where(category:"S").each do |question| %>
     <p><%= question.content %></p>
     <%= render 'answer_fields', :question=> question %>
  <% end %>
<% end %>

      

But I am having trouble passing the original |f|

object from @survey

to new_survey_path

. The last place I can access the form object is in the partial parts of the templates.

How can I fix this?

+3


source to share





All Articles