在rails中使用Ajax时传递Form对象

Jac*_*ham 5 javascript forms ajax ruby-on-rails

我有一个复杂的调查表格.一项调查

belongs_to :template
has_many :questions, :through => :template
has_many :answers, :through => :questions
Run Code Online (Sandbox Code Playgroud)

即用户创建新调查,他必须选择调查模板.调查模板将创建一些默认问题和答案字段.

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

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

<% end %>
Run Code Online (Sandbox Code Playgroud)

然后我渲染_templates部分,我仍然可以访问表单对象.从这里开始,用户可以点击模板名称,模板问题和答案将通过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 %>
Run Code Online (Sandbox Code Playgroud)

调查#新:

respond_to :js, :html

def new
  @template = Template.find(params[:template_id])
  @patient = Patient.find(params[:patient_id])
end
Run Code Online (Sandbox Code Playgroud)

new.js.erb:

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

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 %>
Run Code Online (Sandbox Code Playgroud)

但我有经过最初的故障|f|从对象@surveynew_survey_path.我可以访问表单对象的最后一个位置是模板部分.

我怎样才能解决这个问题?