I have a Product and Category table. Category has_many
Product and Product belongs_to
Category
When I work in console sandbox i can easily get the category a product belongs to by doing:
@p = Product.find(29)
@p.category
However, in the edit page of the Product I am not able to get the category it belongs to.
<% form_for :product, @products do |p| %>
<%= p.error_messages %>
<td><%=label "category", "Category"%></td>
<td><%=p.select :category_id, options_for_select(
@categories.map {|s| [s.name, s.id]},
["#{p.category.id}"])%></td>
So basically i am trying to have edit page for a product with a drop down that contains all categories but i want the current category preselected.
Error I get is:
undefined method `category' for #<ActionView::Helpers::FormBuilder:0xbb35f64>