Devon Quick
Devon Quick

Reputation: 348

Add custom option to select value

I have a select with options filled in from a collection

<%= select('task', 'person_id', Person.where(:job_id => @job.id).order(:name).collect {|p| [p.name, p.id]}, {:include_blank => true}, :required => true)  %>

I'd like to add a 'Not Applicable' option to this select but am unsure how. I have it set to add a blank and I also have it set to required. With both of those true, someone can't just choose the blank. I have the required set because I want my staff to think about the option they select.

Thanks for the help!

Upvotes: 0

Views: 794

Answers (1)

Howlingfish
Howlingfish

Reputation: 301

I'd create a helper method something like this

def person_options(options = {})
  options_for_select([["Not Applicable", ""]] + Person.where(:job_id => @job.id).order(:name).collect {|p| [p.name, p.id]}, options)
end

Then you can call it from erb like so

  <td><%= f.select :person_id, person_options(selected: @person. person_id, include_blank: true), {}, {style: 'width:auto'} %></td>

might need to tweak for your project

Upvotes: 1

Related Questions