gpt4 book ai didi

ruby-on-rails - 如何将参数从 radio_button_tags 传递到 Controller

转载 作者:行者123 更新时间:2023-12-01 22:44:08 25 4
gpt4 key购买 nike

这是我的表单,显示所有 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每个选定的字段 feedback其值为 radio_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"}

最佳答案

好的,答案如下:

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

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com