在数组中发送数据的多个单选按钮组 [英] Multiple radio button groups who send data in an array

查看:42
本文介绍了在数组中发送数据的多个单选按钮组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了以下方法:

<%= 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" %><%结束%>

在我的控制器中,我有一个数组.

现在的问题是我只能选择这四个中的一个,但我想在一组之后选择它们.使用 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" %><%结束%>

有唯一的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" %><%结束%>

但是我现在如何获取参数?

我的控制器包含任何用于测试发送内容的代码:

<预><代码>...定义创建flash[:success] = valueset_params[:value]重定向到 root_path结尾私人的def valueset_paramsparams.permit({value: []})结尾...

希望你明白我的意思.(我必须更改单选按钮的名称,但我仍然希望在我的控制器中接收完整的数组).

如果没有就问吧.

感谢您提出的任何解决方案.

<小时>

编辑另一个问题:

我有类似 @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' %><%结束%>

解决方案

试试这个,让我知道会发生什么:

<%= 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' %><%结束%>

您应该能够使用 params[:value]params[:value][:group_one]params 在控制器中获取数据[:value][:group_two].

I've tried the following:

<%= 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 %>

In my controller i then have an array.

The problem now is that i can only select one of this four, but i want select them after group. With text_fields it works fine but with radio buttons it doesn't work.

Then i tried something like:

<%= 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 %>

to have unique id's but still the same problem. What i need is to have a unique name for every group like:

<%= 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 %>

But how can i get the params now?

My controller contains ony code for test what is send:

...
def create

    flash[:success] = valueset_params[:value]
    redirect_to root_path
end


private

def valueset_params
  params.permit({value: []})
end
...

Hope you understand what i mean. (I have to change the name of the radio buttons and i still want receive the full array in my controller).

Just ask if you don't.

Thanks for any solution propose.


Edit Another question:

I have something like @topics where i have multiple topics inside. Now i want loop them (i know how this works) and write variable values inside the []

<%= 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 %>

解决方案

Try this and let me know what happens:

<%= 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 %>

You should be able to get your data in the controller with params[:value] and then params[:value][:group_one] and params[:value][:group_two].

这篇关于在数组中发送数据的多个单选按钮组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆