collection_radio_buttons:如何将标签包裹在每个单选按钮周围?
collection_radio_buttons: how do you wrap a tag around each radio button?
collection_radio_buttons()
在 rails 5.1 docs 中定义如下:
collection_radio_buttons(
method, collection,
value_method,
text_method,
options = {},
html_options = {}, &block
)
文档中没有解释 options
参数是什么。 simple_form docs 表示有一个名为 item_wrapper_tag
的选项。
我一直在尝试这个:
<%= form_for(:an_article, url: "blah") do |f| %>
<%= f.collection_radio_buttons(
:author_id, Author.all,
:id,
:name_with_initial,
{item_wrapper_tag: :div} #<=== HERE *****
)
%>
<% end %>
我已经尝试了键 item_wrapper_tag
和值 div
的所有符号和字符串组合,但没有成功将每个单选按钮包装在 div 中.
有谁知道 rails 是否有与 item_wrapper_tag
类似的选项?
尝试使用 gem simple_form 作为您的表单。那么下面的代码应该已经可以工作了。
- 在您的
Gemfile
中添加 gem simple_form
。
- 运行
bundle install
- 运行
rails generate simple_form:install
然后在您的视图中创建一个如下所示的 simple_form
:
<%= simple_form_for @post do |f| %>
<%= f.collection_radio_buttons( :author_id, Author.all, :id, :name_with_initial, item_wrapper_tag: :div) %>
<% end %>
注意:我只是按照 APIDock collection_radio_buttons 中的表格进行操作。
这可能会成功。 :)
好的,我明白了:
<%= form_for(:an_article, url: "blah") do |f| %>
<%= f.collection_radio_buttons(
:author_id, Author.all,
:id,
:name_with_initial,
) do |b|
%>
<div>
<%= b.radio_button %>
<%= b.label %>
</div>
<% end %> #collection_radio_buttons do block
<% end %> #form_for do block
radio_button
和 label
对于 |b|uilder 对象是 builtin methods:
The argument passed to the block is a special kind of builder for this
collection, which has the ability to generate the label and radio
button for the current item in the collection... Using it, you can
change the label and radio button display order or even use the label
as wrapper...
附加信息:
collection_radio_buttons(object, method,
collection,
value_method, text_method,
options={}, html_options={}, &block)
collection: For each element in collection, a radio button and label tag is created.
value_method: Called on each element in collection, and the return value is assigned to
the value attribute of the radio button.
object.method: If the return value of object.method is equal to the value attribute of a radio button,
the radio button gets a checked="checked" attribute.
text_method: Called on each element in collection, and the return value is used as
the text for the label tag.
options: Unknown purpose.
html_options: Used to specify additional html attributes for the radio button, e.g. {class: 'group1'}
当你使用form_for()
时,object
参数是f封装的对象,所以省略了对象参数:
f.collection_radio_buttons(method,
collection,
value_method, text_method,
options={}, html_options={}, &block)
和 method
在此对象上被调用:
|
V
form_for(:an_article, url: "blah") do |f|
collection_radio_buttons()
在 rails 5.1 docs 中定义如下:
collection_radio_buttons(
method, collection,
value_method,
text_method,
options = {},
html_options = {}, &block
)
文档中没有解释 options
参数是什么。 simple_form docs 表示有一个名为 item_wrapper_tag
的选项。
我一直在尝试这个:
<%= form_for(:an_article, url: "blah") do |f| %>
<%= f.collection_radio_buttons(
:author_id, Author.all,
:id,
:name_with_initial,
{item_wrapper_tag: :div} #<=== HERE *****
)
%>
<% end %>
我已经尝试了键 item_wrapper_tag
和值 div
的所有符号和字符串组合,但没有成功将每个单选按钮包装在 div 中.
有谁知道 rails 是否有与 item_wrapper_tag
类似的选项?
尝试使用 gem simple_form 作为您的表单。那么下面的代码应该已经可以工作了。
- 在您的
Gemfile
中添加gem simple_form
。 - 运行
bundle install
- 运行
rails generate simple_form:install
然后在您的视图中创建一个如下所示的 simple_form
:
<%= simple_form_for @post do |f| %>
<%= f.collection_radio_buttons( :author_id, Author.all, :id, :name_with_initial, item_wrapper_tag: :div) %>
<% end %>
注意:我只是按照 APIDock collection_radio_buttons 中的表格进行操作。
这可能会成功。 :)
好的,我明白了:
<%= form_for(:an_article, url: "blah") do |f| %>
<%= f.collection_radio_buttons(
:author_id, Author.all,
:id,
:name_with_initial,
) do |b|
%>
<div>
<%= b.radio_button %>
<%= b.label %>
</div>
<% end %> #collection_radio_buttons do block
<% end %> #form_for do block
radio_button
和 label
对于 |b|uilder 对象是 builtin methods:
The argument passed to the block is a special kind of builder for this collection, which has the ability to generate the label and radio button for the current item in the collection... Using it, you can change the label and radio button display order or even use the label as wrapper...
附加信息:
collection_radio_buttons(object, method,
collection,
value_method, text_method,
options={}, html_options={}, &block)
collection: For each element in collection, a radio button and label tag is created.
value_method: Called on each element in collection, and the return value is assigned to
the value attribute of the radio button.
object.method: If the return value of object.method is equal to the value attribute of a radio button,
the radio button gets a checked="checked" attribute.
text_method: Called on each element in collection, and the return value is used as
the text for the label tag.
options: Unknown purpose.
html_options: Used to specify additional html attributes for the radio button, e.g. {class: 'group1'}
当你使用form_for()
时,object
参数是f封装的对象,所以省略了对象参数:
f.collection_radio_buttons(method,
collection,
value_method, text_method,
options={}, html_options={}, &block)
和 method
在此对象上被调用:
|
V
form_for(:an_article, url: "blah") do |f|