simple_form错误地呈现单选按钮

使用simple_form我渲染一些单选按钮以在注册页面上选择Gender。

码:

= f.input :gender, :collection => gender, :as => :radio_buttons, :item_wrapper_class => 'inline' 

这给出了如下输出:

在此处输入图像描述

有没有办法让第一个单选按钮落后于男性和女性背后的第二个单选按钮?

这是一个比我原来的更好的解决方案。

从代码:

 # form_for @user do |f| # f.collection_radio_buttons( # :options, [[true, 'Yes'] ,[false, 'No']], :first, :last # ) do |b| # b.label { b.radio_button + b.text } # end # end 

我认为这就是你要找的东西:

 <%= f.collection_radio_buttons( :gender, [['Male', 'Male'], ['Male', 'Male']], :first, :last ) do |b| b.label { b.text + b.radio_button } end %> 

您甚至可以在其中添加其他帮助程序,如image_tag

 <%= f.collection_radio_buttons( :gender, [['Male', 'Male'], ['Male', 'Male']], :first, :last ) do |b| b.label { image_tag("#{b.text}.png") + b.radio_button } end %> 

此解决方案不是常规自定义组件。 此自定义组件inheritance自CollectRadioButtonsInput而不是普通的CollectionInput类。 我修改了2个相关方法。

app/inputs/radio_buttons_left_label_input.rb

 class RadioButtonsLeftLabelInput < SimpleForm::Inputs::CollectionRadioButtonsInput def input label_method, value_method = detect_collection_methods @builder.send("collection_radio_buttons", attribute_name, collection, value_method, label_method, input_options, input_html_options, &collection_block_for_nested_boolean_style ) end protected def build_nested_boolean_style_item_tag(collection_builder) collection_builder.text.html_safe + collection_builder.radio_button.html_safe end end 

它可以像这样使用

 <%= form.input :gender, :as => :radio_buttons_left_label, :collection => %w[Female Male] %> 

不确定确切的事情,但有一些叫做顺序的东西,可以指定哪个标签首先显示,例如[:label,:input],并且内联可能在这里有所帮助。

您是否尝试按照所需的顺序简单地创建或排序集合?

有人这样想:

 = f.input :gender, :collection => gender.reverse, :as => :radio_buttons, :item_wrapper_class => 'inline' 

显然,这不是更好的解决方案。 您应该按正确的顺序创建列表。