Category can't be blank

Hi,

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:

  • Category can’t be blank

My form code in _form.rhtml is:

Category
<%= collection_select :product, :category_id, @categories, :id, :name, :prompt => "Select a Category" %>

...

My product_controller.rb is:

def new
load_data
@product = Product.new
end

def create
@product = Product.new(params[:product])
if @product.save
flash[:notice] = ‘Product was successfully created.’
redirect_to :action => ‘list’
else
load_data
render :action => ‘new’
end
end

private

def load_data
  @categories = Category.find(:all)
end

My product.rb model is:
class Product < ActiveRecord::Base
belongs_to :categories

file_column :product_image

validates_length_of :title, :in => 1…255
validates_presence_of :category
validates_numericality_of :price
validates_format_of :sku, :with => /[0-9-xX]{9}/
validates_uniqueness_of :sku
end

Please help.
Thanks,
Elle

Because the field name doesn’t really exist , it’s called category_id
and not category in the form?

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

elle wrote:

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

On Sep 7, 4:55 pm, Jamal S. [email protected]
wrote:

Elle

Maybe because you are missing this in your category model.

class Category < ActiveRecord::Base
has_many :products <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
end

Thank you for trying to help me.

My category.rb model is:
class Category < ActiveRecord::Base
has_many :products

validates_length_of :name, :in => 2…255
validates_uniqueness_of :name
end

and the table is really simple. It only has 2 columns: id and name.
Any ideas?

Elle

belongs_to :categories

change to

belongs_to :category

On Sep 7, 5:43 pm, Jamal S. [email protected]
wrote:

belongs_to :categories

change to

belongs_to :category

Jamal, you are a life saver. Thank you so much.
Elle

elle wrote:

On Sep 7, 5:43 pm, Jamal S. [email protected]
wrote:

belongs_to :categories

change to

belongs_to :category

Jamal, you are a life saver. Thank you so much.
Elle

You are welcome :smiley:

Hi elle,

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.

| Privacy Policy | Terms of Service | Remote Ruby Jobs