简体   繁体   English

Rails 3 jQuery更新,其中“ .find params [:id]”不起作用

[英]Rails 3 JQuery update where “.find params[:id]” doesn't work

In a rails 3 app i have a form with 20+ questions and answers. 在Rails 3应用程序中,我有一个包含20多个问题和答案的表单。 The page is populated using a mapped array that has two levels... q[0}.blah is the question array and q[1].yada is the answer array. 该页面使用具有两个级别的映射数组填充... q [0} .blah是问题数组,q [1] .yada是答案数组。 I am using jQuery to submit each changed answer when a radio button is clicked. 单击单选按钮时,我使用jQuery提交每个更改的答案。 Currently I have the following. 目前,我有以下内容。

view: 视图:

<%= 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 %>

controller: (this is wrong) 控制器:(这是错误的)

  def update_result
    @answers = Answer.find params[:id] # THIS LINE PRODUCES THE ERROR BELOW
    @answers.update_attributes params[:score]
  end

I named each radio button set using the answer id. 我使用答案ID为每个单选按钮集命名。 So,... in the console window in which i have the rails server running i get back params consisting of an answer id and the answer for every question: 因此,...在运行Rails服务器的控制台窗口中,我返回由答案ID和每个问题的答案组成的参数:

Parameters: {"22"=>"1", "6"=>"1", "7"=>"1", "12"=>"3", "13"=>"1", "8"=>"2", "14"=>"1", "15"=>"3", "authenticity_token"=>"q2jW71ndaw4vkbTP9qt0Tuo+sOUhdF/Kf4xTf6Up0AY=", "utf8"=>"✓", "16"=>"4", "17"=>"1", "1"=>"1", "18"=>"1", "2"=>"5", "3"=>"4", "19"=>"4", "20"=>"5", "21"=>"1", "4"=>"2", "5"=>"2"}

Then follows the error: 然后出现错误:

ActiveRecord::RecordNotFound (Couldn't find Answer without an ID)

How can i modify the controller code to get the answer id and answer value? 如何修改控制器代码以获取答案ID和答案值? Hidden field maybe? 隐藏的领域也许?

Thanks for your help. 谢谢你的帮助。

UPDATE: Per your request, Mike. 更新:根据您的要求,迈克。 The output... 输出...

._ ._ ._ ._ ._ ._ ._ ._ ._ ._ ._ ._ ._ ._ ._ ._ ._ ._ Some-._ ._ ._ ._ ._ ._ ._ Very ._ ._ ._ ._ ._ ._ ._ ._ ._ ._ ._ ._ ._ ._ ._ ._ ._ ._ ._ ._ ._ ._ ._ ._ ._ ._非常

._ Question._ ._ ._ Never._ ._ Rarely._ ._ times._ ._ ._ Often._ ._ ._ Often ._问题._ ._ ._从不._ ._很少._ ._次._ ._ ._经常._ ._ ._经常

1_ blah blah._ ._ ._ O ._ ._ ._ ._ O ._ ._ ._ ._ O ._ ._ ._ ._ O ._ ._ ._ ._ O 1_等等_._ ._ O ._ ._ ._ ._ O ._ ._ ._ ._ O ._ ._ ._ ._ O ._ ._ ._ ._ O

2_ blah blah._ ._ ._ O ._ ._ ._ ._ O ._ ._ ._ ._ O ._ ._ ._ ._ O ._ ._ ._ ._ O 2_等等_._ ._ O ._ ._ ._ ._ O ._ ._ ._ ._ O ._ ._ ._ ._ O ._ ._ ._ ._ O

3_ blah blah._ ._ ._ O ._ ._ ._ ._ O ._ ._ ._ ._ O ._ ._ ._ ._ O ._ ._ ._ ._ O 3_等等_._ ._ O ._ ._ ._ ._ O ._ ._ ._ ._ O ._ ._ ._ ._ O ._ ._ ._ ._ O

continued 继续

The O's are radio buttons that when clicked should update. O是单选按钮,单击后应更新。

UPDATE 2: per request, route file - 更新2:每个请求,路由文件-

  resources :answers do
    collection do
      post :update_result
    end
  end

what does your routes file look like for the update_result_answers_path ? 对于update_result_answers_path ,您的路线文件是什么样的?

params[:id] doesnt just magically appear unless your route defines it and the object your form is operating on includes it. 除非您的路由定义了params[:id] ,并且您的表单所操作的对象包括它,否则params[:id]不会只是神奇地出现。 You are using a plain form_tag not a form_for _object_ , hence the form isnt operating directly on an object. 您使用的是普通的form_tag而不是form_for _object_ ,因此表单不会直接在对象上运行。

You probably are going to need to pass in an "id" param via jQuery to do what you have listed in your example. 您可能需要通过jQuery传递“ id”参数,以执行示例中列出的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM