这是我的表单,显示了管理员尚未审核的所有 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_optionfeedbackradio_button_tag 的值更新每个选定 12 字段

我现在有你看到的表格,效果很好,但我被困在 Controller 部分:
def moderate_feedbacks
  Feedback.update_all(["review_option = ?", ????])
  redirect_to admin_feedbacks_path
end

我如何将这些单选按钮中的参数传递给 Controller ​​。
谢谢你。

附言 html 源代码:
<input id="review_option_1" name="3" type="radio" value="1">
<input id="review_option_2" name="3" type="radio" value="2">

名称取自 feedback.id
当我按下 submit_tag 时的日志看起来像这样;
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"}

其中 3 是反馈的 id - 2 radio 值,4 是反馈的 id - 1 radio 值

raise.params["feedback.is"].inspect 之后
Parameters:

    {"commit"=>"Apply",
     "3"=>"1",
     "4"=>"1",
     "_method"=>"put"}

最佳答案

好的,这里是答案:

反馈.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

关于ruby-on-rails - 如何将参数从 radio_button_tags 传递给 Controller ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12914440/

10-13 06:42