0

I have a radio button within my form as follows

    <div class="btn-group" data-toggle="buttons-radio">
      <%= f.radio_button :start_year, :class=> "btn", :value=> '2007' %> 2007
      <%= f.radio_button :start_year, :class=> "btn", :value=> '2008' %> 2008
      <%= f.radio_button :start_year, :class=> "btn", :value=> '2009' %> 2009
    </div>

I am using twitter bootstrap

I want to do something like the following:

    <div class="btn-group" data-toggle="buttons-radio">
      <%= f.radio_button :start_year, :class=> "btn", :value=> '2007', if @dates.start_year == 2007 :checked => true end %> 2007
      <%= f.radio_button :start_year, :class=> "btn", :value=> '2008', if @dates.start_year == 2008 :checked => true end %> 2008
      <%= f.radio_button :start_year, :class=> "btn", :value=> '2009', if @dates.start_year == 2009 :checked => true end %> 2009
    </div>

But I get the following error:

syntax error, unexpected keyword_ensure, expecting ')'
syntax error, unexpected keyword_end, expecting ')'

I must be making a mistake in the if statement within the radio button, but I'm not sure how exactly to correct this

Zakoff
  • 12,665
  • 5
  • 22
  • 35

2 Answers2

2

Try

<%= f.radio_button :start_year, :class=> "btn", :value=> '2007', :checked => Proc.new { @dates.start_year == 2007 ? true : false } %> 2007
bensiu
  • 24,660
  • 56
  • 77
  • 117
BvuRVKyUVlViVIc7
  • 11,641
  • 9
  • 59
  • 111
2

I know this is an old question, but maybe it helps others. I don't think we need Proc here, we can just pass boolean value to :checked key. Also, @dates.start_year == 2007 ? true : false can be simplified to @dates.start_year == 2007.

So, the result will be as simple as

<%= f.radio_button :start_year, :value=> '2007', :checked => @dates.start_year == 2007%> 2007
roman-roman
  • 2,746
  • 19
  • 27
  • Note - if you are using a tag (instead of f.), they used different syntax (so you have yet another 'magic' pattern to try to memorize). So for a radio button tag, leave off the ":checked => " and just put a boolean test as the 3rd argument - so: ... radio_button_tag :start_year, :value=> '2007', (@dates.start_year == 2007) – JosephK Sep 21 '17 at 14:40