Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

simple_form render radio button group with <image>( X ) Label with twitter bootstrap?

Im trying for some time to use simple_form to create the below example but + the label and fixing some styling. Anyone knows how to fix:

  • remove the 2's no idea where they come from (seems strange i18n bug on production it throws a whole bunch of I18n in )
  • Have the text Male or Female after the radio button

label output

Is the result from below code:

     .clear
    = f.label "Gender"
    = f.collection_radio_buttons(:gender, [['Male', 'icon_male'], ['Female', 'icon_female']],
                                 :first, :last,
                                 :item_wrapper_class => 'horizontal',
                                 ) do |gender|
      = gender.label { image_tag("/assets/icons/16x16/#{gender.text}.png") + gender.radio_button  }

    .clear
    .ruler
like image 527
Rubytastic Avatar asked Jun 30 '12 08:06

Rubytastic


3 Answers

To remove the 2s and get the text label in there on the right use:

= f.label "Gender"
= f.collection_radio_buttons(:gender, [['Male', 'icon_male'], ['Female', 'icon_female']],
                             :first, :last,
                             :item_wrapper_class => 'horizontal',
                             ) { |gender| gender.label { image_tag("/assets/icons/16x16/#{gender.text}.png") + gender.radio_button + gender.text } }

Removing the = before the gender.label will remove the 2s.

like image 143
23inhouse Avatar answered Sep 24 '22 11:09

23inhouse


Rubytastic you have an extra comma in your answer. Here is the correct code that @23inhouse was suggesting. It works for me in

  • rails 3.1.3
  • simple form 2.0.2
  • haml 3.1.4
  • haml-rails 0.3.4

    = simple_form_for @object_new do |f|
      .clear
      = f.label "Gender"
      - f.collection_radio_buttons(:gender, [['Male', 'icon_male'], ['Female', 'icon_female']], :first, :last, :item_wrapper_class => 'horizontal') do |gender|
        = gender.label { image_tag("rails.png") + gender.radio_button  }
      .clear
      .ruler
like image 35
engineerDave Avatar answered Sep 21 '22 11:09

engineerDave


Final working snippet:

= f.label "Gender"
- f.collection_radio_buttons(:gender, [['Male', 'icon_male'], ['Female', 'icon_female']], :first, :last, :item_wrapper_class => 'horizontal') do |gender|
  = gender.label { image_tag("/assets/icons/16x16/#{gender.text}.png") + gender.radio_button  }
.clear
.ruler
like image 36
Rubytastic Avatar answered Sep 23 '22 11:09

Rubytastic