# Error\_messages\_for trouble

**URL:** <https://www.ruby-forum.com/t/error-messages-for-trouble/68665>\
**Category:** Rails\
**Created:** [February 15, 2006, 5:26pm UTC](https://www.ruby-forum.com/t/error-messages-for-trouble/68665 "2006-02-15T17:26:28Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![scottnj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b9e5f3/32.png) [@scottnj](https://www.ruby-forum.com/u/scottnj)\
**Post date:** [February 15, 2006, 5:26pm UTC](https://www.ruby-forum.com/t/error-messages-for-trouble/68665/1 "2006-02-15T17:26:28Z")

</div>

I am trying so learn how to do custom validations.  
The validation works as expected, but I can’t get “error\_messages\_for”  
to display.  
How is the name of the object supposed to be formated?  
Where is this object created, the model or controller?  
What am I doing wrong here?

#### Model

class MyFolder \< ActiveRecord::Base  
def validate  
# validation test  
if name == ‘test’  
errors.add(:name, “Folder cannot be named test”)  
end  
end  
end

#### Controller

class FolderController \< ApplicationController  
def list  
@my\_folder = MyFolder.new  
end  
def create\_folder  
@my\_folder = MyFolder.new(params[:folder])  
if @my\_folder.save  
flash[:notice] = “Folder created sucessfully”  
else  
flash[:notice] = “Error creating folder”  
end  
redirect\_to :action =\> “list”, :id =\> params[:id]  
end  
end

#### View

\<%= error\_messages\_for(:my\_folder) %\>  
\<%= start\_form\_tag :action =\> ‘create\_folder’ %\>  
\<%= hidden\_field “folder”, “parent\_id”, :value =\> @current\_folder.id %\>  
\<%= text\_field “folder”, “name”, “size” =\> 20 %\>  
\<%= submit\_tag “New Folder” %\>  
\<%= end\_form\_tag %\>

---

<div class="post-metadata">

**Author:** ![scottnj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b9e5f3/32.png) [@scottnj](https://www.ruby-forum.com/u/scottnj)\
**Post date:** [February 15, 2006, 10:55pm UTC](https://www.ruby-forum.com/t/error-messages-for-trouble/68665/2 "2006-02-15T22:55:49Z")

</div>

Does anyone know how to do this? I still can’t figure this out.

I am trying so learn how to do custom validations.  
The validation works as expected, but I can’t get “error\_messages\_for”  
to display.  
How is the name of the object supposed to be formated?  
Where is this object created, the model or controller?  
What am I doing wrong here?

#### Model

class MyFolder \< ActiveRecord::Base  
def validate  
# validation test  
if name == ‘test’  
errors.add(:name, “Folder cannot be named test”)  
end  
end  
end

#### Controller

class FolderController \< ApplicationController  
def list  
@my\_folder = MyFolder.new  
end  
def create\_folder  
@my\_folder = MyFolder.new(params[:folder])  
if @my\_folder.save  
flash[:notice] = “Folder created sucessfully”  
else  
flash[:notice] = “Error creating folder”  
end  
redirect\_to :action =\> “list”, :id =\> params[:id]  
end  
end

#### View

\<%= error\_messages\_for(:my\_folder) %\>  
\<%= start\_form\_tag :action =\> ‘create\_folder’ %\>  
\<%= hidden\_field “folder”, “parent\_id”, :value =\> @current\_folder.id %\>  
\<%= text\_field “folder”, “name”, “size” =\> 20 %\>  
\<%= submit\_tag “New Folder” %\>  
\<%= end\_form\_tag %\>

---

<div class="post-metadata">

**Author:** ![scottnj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b9e5f3/32.png) [@scottnj](https://www.ruby-forum.com/u/scottnj)\
**Post date:** [February 16, 2006, 1:53pm UTC](https://www.ruby-forum.com/t/error-messages-for-trouble/68665/3 "2006-02-16T13:53:55Z")

</div>

> From the documentation examples :  
> errors.add(“phone\_number”, “has invalid format”) unless phone\_number =~  
> /[0-9]\*/

have you tried to use “name” instead of :name ?

class MyFolder \< ActiveRecord::Base  
def validate  
# validation test  
if name == ‘test’  
errors.add(“name”, “Folder cannot be named test”) # name is a  
string not a symbol …  
end  
end  
end

jean

---

<div class="post-metadata">

**Author:** ![scottnj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b9e5f3/32.png) [@scottnj](https://www.ruby-forum.com/u/scottnj)\
**Post date:** [February 16, 2006, 10:48pm UTC](https://www.ruby-forum.com/t/error-messages-for-trouble/68665/4 "2006-02-16T22:48:35Z")

</div>

My problem was I was “redirecting\_to”, when I changed to “render”  
everything worked like I expected.

Jean H. wrote:

> > From the documentation examples :  
> > errors.add(“phone\_number”, “has invalid format”) unless phone\_number =~  
> > /[0-9]\*/
> 
> have you tried to use “name” instead of :name ?
> 
> class MyFolder \< ActiveRecord::Base  
> def validate  
> # validation test  
> if name == ‘test’  
> errors.add(“name”, “Folder cannot be named test”) # name is a  
> string not a symbol …  
> end  
> end  
> end
> 
> jean

---

<div class="post-metadata">

**Author:** ![scottnj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b9e5f3/32.png) [@scottnj](https://www.ruby-forum.com/u/scottnj)\
**Post date:** [February 19, 2006, 7:12am UTC](https://www.ruby-forum.com/t/error-messages-for-trouble/68665/5 "2006-02-19T07:12:55Z")

</div>

The problem with using render instead of redirect\_to is that it breaks  
redirect-on-post.

Try running that page and causing the error. You get the error message,  
right? But now try  
hitting the reload button. You get the “the page … contained post  
data” error from your  
browser, right?

That’s because calling render in the case of error just goes straight to  
the view rather  
than redirecting the browser to the view. So, the post url (the “action”  
attribute" of  
your form) is left in the browser’s address bar.

Now, there are lots of apps out there that do this, so it’s not the end  
of the world. But,  
it’s not very user-friendly. And, you can still redirect and get the  
errors by saving the  
model object or the errors object in the flash. That’s what I did and it  
works fine.

Oh, and I’m also writing this based off of what I saw below… missed  
the begining of the  
thread. So, if I’m completely misunderstanding everything here, just pat  
me on the head,  
give me a cookie and tell me to run along.

b

---

<div class="post-metadata">

**Author:** ![scottnj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b9e5f3/32.png) [@scottnj](https://www.ruby-forum.com/u/scottnj)\
**Post date:** [February 19, 2006, 5:23am UTC](https://www.ruby-forum.com/t/error-messages-for-trouble/68665/6 "2006-02-19T05:23:38Z")

</div>

Thank you for posting your solution. I had the same exact problem,  
using redirect\_to with error\_messages\_for

Once I switched to render it fixed my problem as well.

Thanks!

Scott NJ wrote:

> My problem was I was “redirecting\_to”, when I changed to “render”  
> everything worked like I expected.
> 
> Jean H. wrote:
> 
> > > From the documentation examples :  
> > > errors.add(“phone\_number”, “has invalid format”) unless phone\_number =~  
> > > /[0-9]\*/
> > 
> > have you tried to use “name” instead of :name ?
> > 
> > class MyFolder \< ActiveRecord::Base  
> > def validate  
> > # validation test  
> > if name == ‘test’  
> > errors.add(“name”, “Folder cannot be named test”) # name is a  
> > string not a symbol …  
> > end  
> > end  
> > end
> > 
> > jean

---

<div class="post-metadata">

**Author:** ![scottnj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b9e5f3/32.png) [@scottnj](https://www.ruby-forum.com/u/scottnj)\
**Post date:** [February 19, 2006, 7:40pm UTC](https://www.ruby-forum.com/t/error-messages-for-trouble/68665/7 "2006-02-19T19:40:58Z")

</div>

Scott NJ wrote:

> I am trying so learn how to do custom validations.  
> The validation works as expected, but I can’t get “error\_messages\_for”  
> to display.  
> How is the name of the object supposed to be formated?  
> Where is this object created, the model or controller?  
> What am I doing wrong here?
> 
> #### Model
> 
> class MyFolder \< ActiveRecord::Base  
> def validate  
> # validation test  
> if name == ‘test’  
> errors.add(:name, “Folder cannot be named test”)  
> end  
> end  
> end
> 
> #### Controller
> 
> class FolderController \< ApplicationController  
> def list  
> @my\_folder = MyFolder.new  
> end  
> def create\_folder  
> @my\_folder = MyFolder.new(params[:folder])  
> if @my\_folder.save  
> flash[:notice] = “Folder created sucessfully”  
> else  
> flash[:notice] = “Error creating folder”  
> end  
> redirect\_to :action =\> “list”, :id =\> params[:id]  
> end  
> end
> 
> #### View
> 
> \<%= error\_messages\_for(:my\_folder) %\>  
> \<%= start\_form\_tag :action =\> ‘create\_folder’ %\>  
> \<%= hidden\_field “folder”, “parent\_id”, :value =\> @current\_folder.id %\>  
> \<%= text\_field “folder”, “name”, “size” =\> 20 %\>  
> \<%= submit\_tag “New Folder” %\>  
> \<%= end\_form\_tag %\>

I had this same problem too when using a ‘redirect\_to’. Glad it worked  
out for you!. One other thing you may try is to move the ‘redirect\_to’  
up into your IF statement. That way, when the save works, the user is  
sent to ‘list’. On the flip-side, if the save fails, the form  
(create\_folder) is shown again and the user gets your error messages.
