开发者

In a Rails 3 controller how do i Grab the name of the radio button set that produced the answer?

I've been working on this problem for over 3 days. :=[

I am trying to pick up the values that are being passed from a jQuery. My code for the view, application.js and controller is as follows.

view:

<%= form_tag update_result_answers_path, :remote => true 开发者_StackOverflow中文版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:

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

controller:

  def update_result
    @answ = Answer.where(id = name)
    @answ.update_attributes params[:score]
  end

In the console window where the rails server is running i get the following when a radio button is clicked:

Parameters: {"authenticity_token"=>..., "155"=>"2"}

I named the radio button set with the id of the answer record (see q[1].id in the view). So the name of the radio button set, which is also the id of the answer record, is "155". The answer is "2". Params[:id] doesn't capture the id so i changed to "@answ = Answer.where(id = name)" in the controller and get the following error:

"undefined local variable or method `name'"

How do i change the controller to grab the name of the radio button set and the value?

Thanks.


I'd suggest another level of nesting - so that you can segregate your Answers from other form parameters coming in.

HTML

<%= form_tag update_result_answers_path, :remote => true do %>
  <% @answers.each do |q| %>
    <%= q[0].question %>
    <% [ 1, 2, 3, 4, 5 ].each do |f| %>
  <% if f == q[1].score
    chk="checked='checked'"
  else
    chk=""
  end %>

      <input "<%= chk %>" class="submittable" name="answer[<%=q[1].id%>]" type="radio" value="<%= f %>" />
    <% end %>
  <% end %>
<% end %>

RUBY

def update_result
  params[:answer].each_pair do |key,value|
    @ans = Answer.find(key.to_i)
    @ans.update_attributes(:score => value)
  end
end

I'm not very well versed with Rails view helpers and my solution might be slightly 2.3-ish.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜