Ossie
Ossie

Reputation: 1113

Selected not working with options_from_collection_for_select

line belongs_to :manufacturer

I have the following code in a lines edit view

<%= f.select :manufacturer_id, options_from_collection_for_select(Manufacturer.all, :id, :name, {:selected => @line.manufacturer}) %>

It is not showing the the current @line.manufacturer as selected. Similar code works elsewhere so I can only assume it is a result of using options_from_collection_for_select.

I tried to alter it with reference to the comments here but couldn't get that to work.

What is the solution?

Upvotes: 0

Views: 464

Answers (1)

Carlos Ramirez III
Carlos Ramirez III

Reputation: 7434

The options_from_collection_for_select method signature calls for an integer as the fourth parameter whereas you are passing a hash. Try

<%= f.select :manufacturer_id, options_from_collection_for_select(Manufacturer.all, :id, :name, @line.manufacturer.id) %>

NOTE: As per this comment, the selected parameter needs to be an integer ID.

Upvotes: 3

Related Questions