【问题标题】:Ruby - Radio button form for custom attributeRuby - 自定义属性的单选按钮表单
【发布时间】:2017-10-03 10:46:40
【问题描述】:

假设我有一个这样的课程:

class Something
   has_and_belongs_to_many :categories
end

类类别是这样的:

class Category
   field :name
end

我正在制作一个表单,您可以使用单选按钮选择某物所属的类别,我的问题是如何实现?

我尝试过的如下:

<table>
  <% Category.all.each do |cat| %>
    <tr>
      <td>
        <%= f.label :category, #{cat.name} %>
        <%= f.radio_button, #{cat} %>
      </td>
    </tr>
  <% end %>
</table>

我尝试过使用“#{cat}”或“#{cat.name}”或只是 cat 的其他变体。这些都没有成功。

【问题讨论】:

    标签: ruby-on-rails ruby erb


    【解决方案1】:

    假设这是您的Something 模型的表单(问题上的f.radio_button),您需要在表单上引用category_id,试试这个:

    <table>
      <% Category.all.each do |cat| %>
        <tr>
          <td>
            <%= f.radio_button :category_id, "#{cat.id}", :id => "radio-#{cat.id}" %>
            <label for="radio-#{cat.id}"><%= cat.name %></label>
          </td>
        </tr>
      <% end %>
    </table>
    

    【讨论】:

      猜你喜欢
      • 2014-08-19
      • 1970-01-01
      • 2016-10-03
      • 2019-08-20
      • 1970-01-01
      • 1970-01-01
      • 2019-10-11
      • 2021-04-23
      • 2018-10-08
      相关资源
      最近更新 更多