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

2 Answers 2

2

Try

<%= f.radio_button :start_year, :class=> "btn", :value=> '2007', :checked => Proc.new { @dates.start_year == 2007 ? true : false } %> 2007
Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.