Select i a partial

Hello i got this partial
_form

<%= error_messages_for :product %>

Name
<%= text_field 'product', 'name' %>

Description
<%= text_area 'product', 'description' %>

Category
<%= select(:id,'category',@categories) %>

in the model i got
class Product < ActiveRecord::Base
belongs_to :category
validates_presence_of :name

end

the problem is that i dont see where is the problem to update the cell
“category_id” <<< it always in ull

here is my controller

def update
@product = Product.find(params[:id])
if @product.update_attributes(params[:product])
flash[:notice] = ‘Product ready.’
redirect_to :action => ‘show’, :id => @product
else
render :action => ‘edit’
end
end

I thought when you put “belongs_to” in model, it save the category id
automatically in the db.

Edgar G. wrote:

Hello i got this partial
_form

<%= error_messages_for :product %>

Name
<%= text_field 'product', 'name' %>

Description
<%= text_area 'product', 'description' %>

Category
<%= select(:id,'category',@categories) %>

in the model i got
class Product < ActiveRecord::Base
belongs_to :category
validates_presence_of :name

end

you have to assign the category to the product (the product belongs_to
category).
as i gather from peaking at your post, the selected select-tag that you
have there (selecting from @categories) should be the category_id of the
@product.
so, you have to assign it. this can be done in many different ways. here
are two solutions:

change the select(:id, ‘category’, @categories) to select(:category_id,
‘product’, @categories) # this changes to the correct params, and will
update the correct column (category_id)

in the controller add:
@product.category_id = params[:id] #if the select tag has select(:id
… which means the params[:id] is the value of what was chosen in the
select_tag

hth.

–shai

Wel i got this resolved doing this:

Categoria
<%= select(:producto,'categoria_id',@categorias) %>

:product = the model i am using
category_id = the field i want to fill
@categories = the choices

thanks for all Shai
Edgar G. wrote:

I thought when you put “belongs_to” in model, it save the category id
automatically in the db.