noob
noob

Reputation: 1807

simple form association input: limit options by condition

I have a simple form with the codes:

<%= simple_form_for @business, :html => {:class => "form-inline"} do |f| %>
<%= f.association :business_type, :as => :collection_select, :input_html => {:class => "input-small"}, :label => "Type of Business"%>
<%= f.button :submit, :class => "primary pull-left" %>
<% end %>

In the business type model, I have the following: first_tier, second_tier, third_tier.

I want the select to allow the user to select all options in the business type model, EXCEPT for the first_tier option but couldn't get it to work.

Thanks.

Upvotes: 3

Views: 5423

Answers (1)

rocket scientist
rocket scientist

Reputation: 2447

You can limit the collection options using this:

f.association :business_type, :as => :collection_select, collection: BusinessType.where('biztype <>?', 1), :input_html => {:class => "input-small"}, :label => "Type of Business"

Im not sure what variable you are using to define tier but whatever that variable is just look for when it does not equal first_tier to limit the business type options. MORE INFO can be found in the docs.

Upvotes: 7

Related Questions