使用rails helper转换radiobutton的html代码

时间:2015-10-26 12:42:36

标签: html ruby-on-rails

如何使用rails helper在html中实现以下radiobutton?我的数据库中有一个字段 来宾 ,其中存储了所选的单选按钮值。

<div class="segmented-control" style="width: 100%; color: #5FBAAC">
            <input type="radio" name="guests" id="1">
            <input type="radio" name="guests" id="2">
            <input type="radio" name="guests" id="3">
            <input type="radio" name="guests" id="4">
            <input type="radio" name="guests" id="5">
            <input type="radio" name="guests" id="6">

            <label for="1"  data-value="1">1</label>
            <label for="2" data-value="2">2</label>
            <label for="3" data-value="3">3</label>
            <label for="4"  data-value="4">4</label>
            <label for="5" data-value="5">5</label>
            <label for="6" data-value="6+">6</label>
 </div>

我尝试使用

<%= f.select  :guests, [["1","1"], ["2","2"], ["3","3"], ["4","4"],
["5","5"],["6","6"]], id: "guests", class: "form-control" %>

但没有工作

2 个答案:

答案 0 :(得分:1)

有一个名为options_for_select的辅助方法,它将数组数组转换为选择输入选项。

<%= f.select :guests, options_for_select([["1","1"],["2","2"],["3","3"],["4","4"],
["5","5"],["6","6"]]), id: "guests", class: "form-control" %>

文档:http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-options_for_select

答案 1 :(得分:0)

我认为它会返回你想要的东西:

<%1.upto(6) do |n|%>
   <%= radio_button_tag :guests, "#{n}",nil, id: "#{n}" %>
<%end%>

<%1.upto(6) do |n|%>
   <%= label_tag("#{n}", "#{n}", "data-tag"=>"#{n}") %>
<%end%>