Forum: Ruby In f.select, how do I set a default value if :state is blank?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
7a096d30179ceb77d76d65c0eddc1d98?d=identicon&s=25 Charlie Lor (chuck)
on 2016-10-03 21:57
Below my current code. I want to default to "WI" if it's a new record.

<%= f.select :state, Address.state_select, :state? ? 'WI' : :state %>

This does not work. It gave me the following error:

ctionView::Template::Error (undefined method `merge' for "WI":String):
    13:     </p>
    14:     <p>
    15:       <%= f.label :state %>
    16:       <%= f.select :state, Address.state_select, :state? ? 'WI'
: :state %>
    17:     </p>
    18:     <p>
    19:       <%= f.label :zip %>
0fa73332c8e4a3b06ea439fd3f034322?d=identicon&s=25 Ronald Fischer (rovf)
on 2016-10-07 14:15
First of all, I don't see any invocation of a method 'merge' in your
code, so I can't comment on that.

What *is* odd, is the expression

    :state? ? 'WI' : :state

It mentions two different symbols: :state? and :state. The condition is
just :state?. If you use a symbol as a condition, it is always
considered true ("every symbol is true"). Hence, the whole expression
always returns the string 'WI'.
This topic is locked and can not be replied to.