Hirurg103
Hirurg103

Reputation: 4953

Include blank option on a condition in select

I have to display list of countries which can be rated. If a country has already been rated then users cannot "unrate" it

# countries index
= render @countries

# countries/_country
= form_for country do |f|
  =  f.select :rating,
    Rating.options,
    include_blank: "False if country is rated. True otherwise"

I tried to pass a proc to include_blank option but it didn't work

I can use helper to calculate options which looks like this:

def rating_options(country)
   if country.rating.present?
     Rating.options
   else
     [""] + Rating.options
   end
end

Is there a better way to include blank option in the select tag on a condition?

Upvotes: 1

Views: 907

Answers (1)

Michael Yurkovetc
Michael Yurkovetc

Reputation: 156

You can pass to include_blank option any helper methods or conditions

= form_for country do |f|
  = f.select :rating,
    Rating.options,
    include_blank: f.object.rating.empty?

Upvotes: 1

Related Questions