开发者

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

开发者 https://www.devze.com 2023-03-04 16:41 出处:网络
In a rails 3 app i have a form with 20+ questions and answers. 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. I am

In a rails 3 app i have a form with 20+ questions and answers. 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. I am using jQuery to submit each changed answer when a radio button is clicked. 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 %>

cont开发者_开发问答roller: (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. 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:

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? 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

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

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

continued

The O's are radio buttons that when clicked should update.

UPDATE 2: per request, route file -

  resources :answers do
    collection do
      post :update_result
    end
  end


what does your routes file look like for the 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. You are using a plain form_tag not a form_for _object_, hence the form isnt operating directly on an object.

You probably are going to need to pass in an "id" param via jQuery to do what you have listed in your example.

0

精彩评论

暂无评论...
验证码 换一张
取 消