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

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:


14:


15: <%= f.label :state %>
16: <%= f.select :state, Address.state_select, :state? ? ‘WI’
: :state %>
17:


18:


19: <%= f.label :zip %>

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 forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs