Problems with displaying validation message

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
.....................................

Don't use button_to: that won't submit your form. Use submit_tag if you want a submit button.

Fred

Frederick Cheung wrote:

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.

Thanks

Hi,

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
.....................................