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: ... <p><label for="product_category">Category</label><br/> <%= collection_select :product, :category_id, @categories, :id, :name, :prompt => "Select a Category" %></p> ... 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Because the field name doesn''t really exist , it''s called category_id and not category in the form? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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? > > ElleMaybe because you are missing this in your category model. class Category < ActiveRecord::Base has_many :products <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< end -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Sep 7, 4:55 pm, Jamal Soueidan <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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 > --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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
belongs_to :categories change to belongs_to :category -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Sep 7, 5:43 pm, Jamal Soueidan <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> belongs_to :categories > > change to > > belongs_to :categoryJamal, you are a life saver. Thank you so much. Elle --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
elle wrote:> On Sep 7, 5:43 pm, Jamal Soueidan <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: >> belongs_to :categories >> >> change to >> >> belongs_to :category > > > > Jamal, you are a life saver. Thank you so much. > ElleYou are welcome :D -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 On Sep 7, 6:24 am, elle <wazne...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Sep 7, 4:55 pm, Jamal Soueidan <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: > > > > > > > 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 > > -- > > 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:idand name. > Any ideas? > > Elle- Hide quoted text - > > - Show quoted text ---~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---