如何将params从radio_button_tags传递给控制器

时间:2012-10-16 12:12:33

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.1 ruby-on-rails-3.2

这是我的表单,显示管理员尚未审核的所有feedbacks

每个feedback旁边有2个单选按钮,可选择接受或拒绝,其值为12

<% form_tag moderate_feedbacks_path, :method => :put do %>
  <table>
    <% @all_feedbacks.each do |feedback| %>
    <tr>
      <td><%= radio_button_tag :review_option, '1', false, :name => feedback.id %></td>
      <td><%= radio_button_tag :review_option, '2', false, :name => feedback.id %></td>
      <td><%= feedback.name %></td>
      <td><%= feedback.email %></td>
      <td><%= feedback.message %></td>
    </tr>
    <% end %>
   </table>
<%= submit_tag 'Apply' %>
<% end -%>

当我点击submit_tag时,我想要做的是更新每个选定review_option的{​​{1}}字段,其值为feedback,{{1} }或radio_button_tag

我现在看到它的形式,效果很好,但我被困在控制器部分:

1

如何将这些单选按钮中的参数传递给控制器​​。 谢谢。

p.s。 html来源:

2

名称取自def moderate_feedbacks Feedback.update_all(["review_option = ?", ????]) redirect_to admin_feedbacks_path end

按下submit_tag时的

日志看起来像这样;

<input id="review_option_1" name="3" type="radio" value="1">
<input id="review_option_2" name="3" type="radio" value="2">

其中3是反馈的id - 2是无线电值,4是反馈的id - 1是无线电值

feedback.id

之后

Processing Admin::FeedbacksController#moderate_feedbacks (for 127.0.0.1 at 2012-10-16 15:36:20) [PUT]
  Parameters: {"commit"=>"Apply", "3"=>"2", "4"=>"1"}

2 个答案:

答案 0 :(得分:1)

当我不确定如何在params中传递数据时,我喜欢在控制器中引发异常。开发异常处理程序输出有关params内容的非常好的信息。我甚至可以为我的例外提出.inspect并查看详细信息。

def update
  raise "some string"
end

def update
  raise params["feedback.id"].inspect
end

另请注意,radio_button_tag的第一个参数是名称,因此您不必在选项中传递它。

答案 1 :(得分:1)

好的,这就是答案:

feedback.rb

中的

   class Status
     ACCEPTED = 1
     REJECTED = 2
   end

格式

<% form_tag moderate_feedbacks_path, :method => :put do %>
  <table>
    <% @all_feedbacks.each do |feedback| %>
    <tr>
      <td><%= radio_button_tag :review_option, Feedback::Status::ACCEPTED, false, :name => feedback.id %></td>
      <td><%= radio_button_tag :review_option, Feedback::Status::REJECTED, false, :name => feedback.id %></td>
      <td><%= feedback.name %></td>
      <td><%= feedback.email %></td>
      <td><%= feedback.message %></td>
    </tr>
    <% end %>
   </table>
<%= submit_tag 'Apply' %>
<% end -%>
feedbacks_controller.rb

中的

   def moderate_feedbacks
    params.each do |key, value|
      if key =~ /^r(\d+)/ && !value.blank?
        feedback_id = $1
        Feedback.update_all(["review_option = ?", value.to_i], ["id = ?", feedback_id])
      end
    end
    redirect_to admin_feedbacks_path
  end
相关问题