I’m trying to create a new product and associate a category with the
new product. The categories are loaded into a form select field. When
I try try to create a new product, I get the error:
Also, for some reason (maybe it’s the reason the above doesn’t work)
is that I get this error:
undefined method `category’ for #Product:0x344b170
when I’m trying to define: product.category.name
Why can’t it see the category’s name that is related to the product?
Also, for some reason (maybe it’s the reason the above doesn’t work)
is that I get this error:
undefined method `category’ for #Product:0x344b170
when I’m trying to define: product.category.name
Why can’t it see the category’s name that is related to the product?
Any help? Please?
Elle
Maybe because you are missing this in your category model.
class Category < ActiveRecord::Base
has_many :products <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
end
Rails save method as you are using it will only save the selected id
key from category into the category_id field in your Product table but
will not save the category name into the “category” field.
So validates_presence_of :category in your Product model will give an
error because there is no data saved into the category field.
You can check this by looking in your database table and you will see
no data in the category field but the id field has the category id.
Try your code without the validates_presence_of :category.
I am very new to Rails - a few weeks!!! so I dont have the complete
answer - I hope this piece is helpful.
I also have the same problem and must find a way of populating my
category name field as well as the id field!!
Any helpers??
euro
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.