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
javascript ajax ruby-on-rails forms


source to share


No one has answered this question yet

Check out similar questions:

5722
How can I remove a property from a JavaScript object?
5101
What's the most efficient way to deeply clone an object in JavaScript?
3714
How to check if an array contains a value in JavaScript?
2886
How do I clone a JavaScript object correctly?
2709
Detecting object property undefined
2685
Checking if a key exists in a JavaScript object?
2543
How can I check if an object is an array?
2350
Storing objects in HTML5 localStorage
1744
Cancel Ajax requests with jQuery
1567
Converting form data to JavaScript object with jQuery



All Articles
Loading...
X
Show
Funny
Dev
Pics