I have a line like this:
<%= f.input :state_id, :input_html => {:value => (policy_address.state.name rescue nil)}, :required => true, :collection => states.map {|s| [ s.name, s.id] }, :include_blank => 'Please select'%>
I want to exclude a value from the states.map collection. I thought that this would work but it doesn’t:
<%= f.input :state_id, :input_html => {:value => (policy_address.state.name rescue nil)}, :required => true, :collection => states.map {|s| [ s.name, s.id] unless s.name == "excluded_state" }, :include_blank => 'Please select'%>
I put in unless s.name == "excluded_state, but, again, it’s not working:
What am I doing wrong?
mapdoesn’t allow to skip values. You have to reject unwanted elements first.Another (dirtier) solution is to return
nilfor excluded elements and useArray#compacton the resulting array to remove thosenilelements: