我尝试了以下内容:
<%= form_for .... do |f| %>
<%= f.label "test1" %>
<%= radio_button_tag 'value[]', "1" %>
<%= radio_button_tag 'value[]', "2" %>
<%= f.label "test2" %>
<%= radio_button_tag "value[]", "1" %>
<%= radio_button_tag "value[]", "2" %>
<%= f.submit "test_send" %>
<% end %>
在我的控制器中,我有一个数组。
现在的问题是,我只能选择这四个中的一个,但我希望在群组后选择它们。使用text_fields
它可以正常工作,但使用单选按钮它不起作用。
然后我尝试了类似的事情:
<%= form_for .... do |f| %>
<%= f.label "test1" %>
<%= radio_button_tag 'value[]', "1", :id => "btn-grp-1" %>
<%= radio_button_tag 'value[]', "2", :id => "btn-grp-2" %>
<%= f.label "test2" %>
<%= radio_button_tag "value[]", "1", :id => "btn-grp-3" %>
<%= radio_button_tag "value[]", "2", :id => "btn-grp-4" %>
<%= f.submit "test_send" %>
<% end %>
拥有唯一的ID,但仍然是同一个问题。 我需要的是为每个组创建一个唯一的名称,如:
<%= form_for .... do |f| %>
<%= f.label "test1" %>
<%= radio_button_tag 'value[1]', "1", :id => "btn-grp-1" %>
<%= radio_button_tag 'value[1]', "2", :id => "btn-grp-2" %>
<%= f.label "test2" %>
<%= radio_button_tag "value[2]", "1", :id => "btn-grp-3" %>
<%= radio_button_tag "value[2]", "2", :id => "btn-grp-4" %>
<%= f.submit "test_send" %>
<% end %>
但我现在如何获得参数呢?
我的控制器包含用于测试发送内容的ony代码:
...
def create
flash[:success] = valueset_params[:value]
redirect_to root_path
end
private
def valueset_params
params.permit({value: []})
end
...
希望你理解我的意思。 (我必须更改单选按钮的名称,我仍然希望在控制器中接收完整的数组。)
请问你是不是。
感谢您提出任何解决方案。
修改另一个问题:
我有类似@topics
的内容,其中我有多个主题。
现在我想循环它们(我知道它是如何工作的)并在[]
<%= form_for .... do |f| %>
<%= f.label 'test1' %>
<%= radio_button_tag 'value[@topic1.id]', '1' %>
<%= radio_button_tag 'value[@topic1.id]', '2' %>
<%= f.label 'test2' %>
<%= radio_button_tag 'value[@topic2.id]', '1' %>
<%= radio_button_tag 'value[@topic2.id]', '2' %>
<%= f.submit 'test_send' %>
<% end %>
答案 0 :(得分:6)
试试这个,让我知道会发生什么:
<%= form_for .... do |f| %>
<%= f.label 'test1' %>
<%= radio_button_tag 'value[group_one]', '1' %>
<%= radio_button_tag 'value[group_one]', '2' %>
<%= f.label 'test2' %>
<%= radio_button_tag 'value[group_two]', '1' %>
<%= radio_button_tag 'value[group_two]', '2' %>
<%= f.submit 'test_send' %>
<% end %>
您应该能够使用params[:value]
然后params[:value][:group_one]
和params[:value][:group_two]
在控制器中获取数据。