造型 'collection_radio_buttons'

Styling 'collection_radio_buttons'

我能否将这些样式设置为在集合中的每个单选按钮之间出现换行符?

f.collection_radio_buttons(
  :chosen,
  [['A1', 1],['A2', 2], ['A3', 3]],
  :last,
  :first,
  html_options: { class: 'form-control' }
)

我无法测试,但我认为这可以帮助:

<%= f.collection_radio_buttons(:chosen, [['A1', 1],['A2', 2], ['A3', 3]], :last, :first, html_options: { class: 'form-control' }) do |b| %>
    <%= b.label { b.radio_button + " " + b.text } %><br>
<% end %>

看起来 form_tag 版本允许通过块进行自定义,所以我认为这同样适用于 form_for

http://apidock.com/rails/v4.0.2/ActionView/Helpers/FormOptionsHelper/collection_radio_buttons

It's also possible to customize the way the elements will be shown by giving a block to the method:

collection_radio_buttons(:post, :author_id, Author.all, :id, :name_with_initial) do |b|
  b.label { b.radio_button }
end

所以你可以尝试这样的事情

<%= f.collection_radio_buttons(:chosen, [['A1', 1],['A2', 2], ['A3', 3]], :last, :first, html_options: { class: 'form-control' }) do |b| %>
  <%= b.label { b.radio_button } %><br>
<% end %>

我是这样实现的:

= residency.collection_radio_buttons :own,
    [[true, 'I Own This Home'], [nil, 'I Rent'], [false, 'I Live Rent Free']],
    :first,
    :last,
    item_wrapper_tag: :div, <-- MAGIC IS HERE
    label: false do |rdb|
      .d-block
        = rdb.radio_button + rdb.label(class: 'p-l-1')