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’ %>

Create New Book

<% 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…

Name: <%= f.text_field :name %>

Address: <%= f.text_field :author %>

Phone Number: <%= f.text_field :price %>

..................................

…error message…
Called id for nil, which would mistakenly be 4 – if you really wanted
the id of nil, use object_id

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.

Fred

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

Fred

Hi,

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

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’ %>

Create New Book

<% 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…

Name: <%= f.text_field :name %>

Address: <%= f.text_field :author %>

Phone Number: <%= f.text_field :price %>

..................................

…error message…
Called id for nil, which would mistakenly be 4 – if you really wanted
the id of nil, use object_id