# Problems with displaying validation message

**URL:** <https://www.ruby-forum.com/t/problems-with-displaying-validation-message/135128>\
**Category:** Rails\
**Created:** [April 18, 2008, 7:53am UTC](https://www.ruby-forum.com/t/problems-with-displaying-validation-message/135128 "2008-04-18T07:53:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![fries88](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/6bbea6/32.png) [@fries88](https://www.ruby-forum.com/u/fries88)\
**Post date:** [April 18, 2008, 7:53am UTC](https://www.ruby-forum.com/t/problems-with-displaying-validation-message/135128/1 "2008-04-18T07:53:32Z")

</div>

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  
…

---

<div class="post-metadata">

**Author:** ![fries88](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/6bbea6/32.png) [@fries88](https://www.ruby-forum.com/u/fries88)\
**Post date:** [April 18, 2008, 11:17am UTC](https://www.ruby-forum.com/t/problems-with-displaying-validation-message/135128/2 "2008-04-18T11:17:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![fries88](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/6bbea6/32.png) [@fries88](https://www.ruby-forum.com/u/fries88)\
**Post date:** [April 19, 2008, 1:33pm UTC](https://www.ruby-forum.com/t/problems-with-displaying-validation-message/135128/3 "2008-04-19T13:33:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![fries88](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/6bbea6/32.png) [@fries88](https://www.ruby-forum.com/u/fries88)\
**Post date:** [April 21, 2008, 3:20am UTC](https://www.ruby-forum.com/t/problems-with-displaying-validation-message/135128/4 "2008-04-21T03:20:03Z")

</div>

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  
…
