I have two objects ingredient & origin.
each ingredient has an origin so in the ingredient I have origin_id
the view displays
<p>
<b>Name:</b>
<%= @ingredient.name %>
</p>
<p>
<b>Origin:</b>
<%= @ingredient.origin_id %>
</p>
class ingredient is declared as follows
class Ingredient < ActiveRecord::Base
has_and_belongs_to_many :recipes
belongs_to :origin
attr_accessible :name, :origin_id
end
class origin
class Origin < ActiveRecord::Base
attr_accessible :name
end
in the edit form I write and it works
<% originsArray = Origin.all.map { |origin| [origin.name, origin.id] } %>
<div class="field">
<%= f.label :origin_id %><br/>
<%= f.select(:origin_id, originsArray) %><br/>
</div>
but if I write instead (as in all tutorials)
<%= collection_select(:origin, :id, @origins, :id, :name, options ={:prompt => "-Select a payment"}, :class =>"origin") %>
i get
undefined method `map' for nil:NilClass
what should I fix ?
Edit
added :
def edit
@ingredient = Ingredient.find(params[:id])
@origins = Origin.all
end
I don’t see here where @origins is declarated. It seems like your @origins is just nil and not collection.