Zakoff
Zakoff

Reputation: 12995

Rails 3, checked radio button with if function

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

Upvotes: 0

Views: 5020

Answers (2)

roman-roman
roman-roman

Reputation: 2796

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

Upvotes: 2

BvuRVKyUVlViVIc7
BvuRVKyUVlViVIc7

Reputation: 11811

Try

<%= f.radio_button :start_year, :class=> "btn", :value=> '2007', :checked => Proc.new { @dates.start_year == 2007 ? true : false } %> 2007

Upvotes: 2

Related Questions