你能为jQuery,Rails 3更新渲染一个包含多个表单而不是一个表单的页面吗? [英] Can you render a page with multiple forms instead of one form for a jQuery, Rails 3 update?

查看:105
本文介绍了你能为jQuery,Rails 3更新渲染一个包含多个表单而不是一个表单的页面吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有100多个问题的rails 3表单。每次单击与答案关联的单选按钮时,表单将返回所有100多个问题的答案。因此,当某人完成时,将为所有100个问题更新100条记录。共有10,000条记录更新。

I have a rails 3 form that has 100+ questions. Each time a radio button associated with an answer is clicked the form returns the answers for all 100+ questions. So that would be 100 records updated for all 100 questions by the time someone is finished. A total of 10,000 record updates.

我正在考虑如何更改代码,以便每次点击只更新一个答案而不是全部100 +。

I am trying to think through how to change the code to update only one answer per click instead of all 100+.

如果你想了解更多细节,代码如下。谢谢。

The code is below if you want to go into that much detail. Thanks.

表格:

<%= form_tag update_result_answers_path, :remote => true do %>
   <% @answers.each do |q| %>
      <%= q[0].question %>
      <% [ 1, 2, 3, 4, 5 ].each do |f| %>
         <%= radio_button_tag q[1].id, f, f == q[1].score, :class => 'submittable' %>
      <% end %>
   <% end %>
<% end %>

application.js:

application.js:

$('.submittable').live('change', function() {
$(this).parents('form:first').submit();
return false;
});

控制器:

  def update_result
    @answers = []

    params[:answer].each_pair do |key,value|
      ans = Answer.find(key.to_i)        
      ans.update_attributes(:score => value)
      @answers << ans
    end
    render :nothing => true
  end


推荐答案

好的,我想我现在遇到了问题。
他就是我要做的事情:

Okay, I think I got the problem now. He is what I would do:

<%= form_tag update_score_answer_path(answer), :id => "answer_#{answer.id}_form", :remote => true do %>
  <%= answer.question %> <%# didn't understand this here, maybe it's outside of the loop? %>
  <% [ 1, 2, 3, 4, 5 ].each do |f| %>
    <%= radio_button_tag answer.id, f, f == answer.score, :class => 'submittable' %>
  <% end %>
<% end %>

然后循环播放:

<% @answers.each do |answer| %>
  <%= render 'answers/answer_form', :answer => answer %>
<% end %>

然后在控制器上

def update_score
  @answer = Answer.find(params[:answer_id])
  @answer.score = params[:answer][:score]
  @answer.save
end

并以js回复

$('#answer_<%= @answer.id %>_form').replaceWith("<%= escape_javascript( render 'answers/answer_form', :answer => @answer ) %>")

控制器可能不正确,但是你得到照片。如果你愿意,你也可以更新单选按钮,只需返回javascript。

The controller is probably not correct, but you get the picture. you can also just update the radio button if you'd like, just javascript being returned.

希望这有帮助

此外,如果需要,您可以使用

Also, if needed, you can switch the renders with

render :partial => 'lala', :locals => { :answer => object }

这篇关于你能为jQuery,Rails 3更新渲染一个包含多个表单而不是一个表单的页面吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆