[英]collection_radio_buttons: how do you wrap a tag around each radio button?
collection_radio_buttons()
is defined in the rails 5.1 docs like this: 在rails 5.1文档中定义了
collection_radio_buttons()
,如下所示:
collection_radio_buttons(
method, collection,
value_method,
text_method,
options = {},
html_options = {}, &block
)
There is no explanation in the docs for what the options
argument is. 在文档中没有关于
options
参数是什么的解释。 The simple_form docs say that there is an option called item_wrapper_tag
. simple_form文档说有一个名为
item_wrapper_tag
的选项。
I've been trying this: 我一直在尝试:
<%= 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 %>
I've tried every combination of symbols and strings for the key, item_wrapper_tag
, and the value, div
, and nothing succeeds in wrapping each radio button in a div. 我已经尝试过键和
item_wrapper_tag
以及值div
的符号和字符串的每种组合, item_wrapper_tag
没有成功地将每个单选按钮包装在div中。
Does anyone know if rails has a similar option as item_wrapper_tag
? 有谁知道rails是否具有与
item_wrapper_tag
类似的选项?
Try using the gem simple_form for your forms. 尝试为表单使用gem simple_form 。 Then the code below should work already.
那么下面的代码应该已经可以工作了。
gem simple_form
in your Gemfile
. Gemfile
添加gem simple_form
Gemfile
。 bundle install
bundle install
rails generate simple_form:install
rails generate simple_form:install
Then create a simple_form
in your view that would look like this: 然后在您的视图中创建一个
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 %>
Note: I just followed the form from the collection_radio_buttons from APIDock. 注意:我只是遵循APIDock的collection_radio_buttons中的表格。
This might do the trick. 这可能会成功。 :)
:)
Okay, I figured it out: 好的,我知道了:
<%= 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
and label
are builtin methods for the |b|uilder object: radio_button
和label
是| b | uilder对象的内置方法 :
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...
传递给块的参数是此集合的一种特殊类型的生成器,它能够为集合中的当前项目生成标签和单选按钮。使用它,您可以更改标签和单选按钮的显示顺序甚至使用标签作为包装纸...
Additional info: 附加信息:
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'}
When you use form_for()
, the object
argument is the object encapsulated by f, so you omit the object argument: 当使用
form_for()
, object
参数是f封装的对象,因此可以省略object参数:
f.collection_radio_buttons(method,
collection,
value_method, text_method,
options={}, html_options={}, &block)
and method
is called on this object: 在此对象上调用和
method
:
|
V
form_for(:an_article, url: "blah") do |f|
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.