Hi, Sorry I didn''t post the right thing. I have two tables category and book. I have a hidden category_id field in the create book page. So by right if I submit an empty create book form, an error message box will show on the top of the page displaying my errors. But instead, I got the following error. Where did it go wrong. Please help. Have been stuck for days. I have also tried to use form_for with render partial but I can''t submit my form. It just redirect back to the same page. Thanks ..........category table........ id integer (primary key) cat_name varchar validates_presence_of :name .........book table............. id integer(primary key) name varchar author varchar price float category_id integer(foreign key) validates_presence_of :name, :author ..........category model............ class Category < ActiveRecord::Base has_many: books end .........book model.............. class Book < ActiveRecord::Base belongs_to: category end .....book.rhtml (view)............. <%= error_messages_for ''book'' %> <b><u>Create New Book</u></b><p> <% form_for :book, :action => :save_book, :html => {:id => :book} do |f| %> <%= f.hidden_field :category_id, :value => @category.id %> <%= button_to "Create", :action => :save_book, :id => :book %> <% end %> ....................................... ......_form.rhtml................ <p>Name: <%= f.text_field :name %><p/> <p>Address: <%= f.text_field :author %><p/> <p>Phone Number: <%= f.text_field :price %><p/> .................................. .........error message............... Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id ..................................... -- 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 18 Apr 2008, at 06:53, user splash wrote:> > Hi, > > Sorry I didn''t post the right thing. I have two tables category and > book. I have a hidden category_id field in the create book page. So > by > right if I submit an empty create book form, > an error message box will show on the top of the page displaying my > errors. But instead, I got the following error. Where did it go wrong. > > Please help. Have been stuck for days. > > I have also tried to use form_for with render partial but I can''t > submit > my form. It just redirect back to the same page. > > <% form_for :book, :action => :save_book, :html => {:id => :book} do > |f| > %> > > > <%= f.hidden_field :category_id, :value => @category.id %> > > <%= button_to "Create", :action => :save_book, :id => :book %>Don''t use button_to: that won''t submit your form. Use submit_tag if you want a submit button. Fred --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:> On 18 Apr 2008, at 06:53, user splash wrote: > >> Please help. Have been stuck for days. >> <%= f.hidden_field :category_id, :value => @category.id %> >> >> <%= button_to "Create", :action => :save_book, :id => :book %> > > Don''t use button_to: that won''t submit your form. Use submit_tag if > you want a submit button. > > FredHi, I tried the submit tag. But nothing happen when the button is being clicked. I''m still having problems with the validation. Please help. I don''t know where went wrong. There are no problems when all fields are filled and filled with the right data types. Thanks -- 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 -~----------~----~----~----~------~----~------~--~---
user splash wrote:> Hi, > > I tried the submit tag. But nothing happen when the button is being > filled and filled with the right data types. > > ThanksHi, I have no problems using the form_for now. But I''m still having problems with the validation I don''t know where went wrong. There are no problems when all fields are filled and filled with the right data types. The following are my model, view, error message and table: Please help. Thanks ..........category table........ id integer (primary key) cat_name varchar validates_presence_of :name .........book table............. id integer(primary key) name varchar author varchar price float category_id integer(foreign key) validates_presence_of :name, :author ..........category model............ class Category < ActiveRecord::Base has_many: books end .........book model.............. class Book < ActiveRecord::Base belongs_to: category end .....book.rhtml (view)............. <%= error_messages_for ''book'' %> <b><u>Create New Book</u></b><p> <% form_for :book, :action => :save_book, :html => {:id => :book} do |f| %> <%= f.hidden_field :category_id, :value => @category.id %> <%= button_to "Create", :action => :save_book, :id => :book %> <% end %> ....................................... ......_form.rhtml................ <p>Name: <%= f.text_field :name %><p/> <p>Address: <%= f.text_field :author %><p/> <p>Phone Number: <%= f.text_field :price %><p/> .................................. .........error message............... Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id ..................................... -- 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 -~----------~----~----~----~------~----~------~--~---