views:

17

answers:

1

Hi all,

I have a form partial that is being called in a content_for :sidebar. This collection_select should have "selected" set if the page calling the partial is a specific package page. Otherwise, it should have a "prompt" to select. How would I DRY this up? I tried an inline ternary on a single collection_select to no avail.

<%- if @package.blank? -%>
    <%= f.collection_select :package_name, Package.all, :name, :name, :prompt => "Please Select"  %>
<%- else -%>
    <%= f.collection_select :package_name, Package.all, :name, :name, :selected => @package.name %>
<%- end -%>

Thanks

A: 

How about:

<%= f.collection_select :package_name, Package.all, :name, :name, 
      @package.blank? ? { :prompt => "Please Select" } : { :selected => @package.name } %>
Shadwell
Hmm...not quite. That's what I tried before and still get a syntax error.
Matt
I figured it out. The conditional options need to be specified as a hash with curly brackets. This is the correct syntax:<%= f.collection_select :package_name, Package.all, :name, :name, @package.blank? ? {:prompt => "Please Select"} : {:selected => @package.name} %>
Matt
Nice one. I've edited my answer to reflect that.
Shadwell