# How to display the error messages from the controller

**URL:** <https://www.ruby-forum.com/t/how-to-display-the-error-messages-from-the-controller/52170>\
**Category:** Rails\
**Created:** [January 11, 2006, 5:57pm UTC](https://www.ruby-forum.com/t/how-to-display-the-error-messages-from-the-controller/52170 "2006-01-11T17:57:52Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![nekapoor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ea666f/32.png) [@nekapoor](https://www.ruby-forum.com/u/nekapoor)\
**Post date:** [January 11, 2006, 5:57pm UTC](https://www.ruby-forum.com/t/how-to-display-the-error-messages-from-the-controller/52170/1 "2006-01-11T17:57:52Z")

</div>

I have a wizard which consists of two pages. To keep it simple let’s say  
that the first page asks the user to select a state from the drop down  
menu.  
In case the user doesn’t select any state, an error message needs to be  
displayed.

Ideally I would like to do something like this.

def new  
selected\_value = params[:addevent][:state\_id]  
if selected\_value == “-1”  
errors.add(:state\_id,“Please select a valid state”)  
else  
# go to the next page  
end  
end

But errors is not available in the controller. It’s available only in  
the  
model. I could use flash[:notice] but using errors would have have  
helped.  
It would have kept everything consistent with the other error message.

Anything I’m missing here.

Thanks.

---

<div class="post-metadata">

**Author:** ![nekapoor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ea666f/32.png) [@nekapoor](https://www.ruby-forum.com/u/nekapoor)\
**Post date:** [January 11, 2006, 7:15pm UTC](https://www.ruby-forum.com/t/how-to-display-the-error-messages-from-the-controller/52170/2 "2006-01-11T19:15:17Z")

</div>

On Jan 11, 2006, at 9:56 AM, Neeraj K. wrote:

> ```
> errors.add(:state_id,"Please select a valid state")
> 
> ```

I’m not sure I understand why your controller has to add the error  
message. Does state\_id belong to a model, or is it kind of  
“floating” as unrelated data?

If it belongs to a model, I would do the check inside your model by  
overriding the “validate” method.

e.g.

class User \< ActiveRecord::Base  
def validate  
if state\_id == -1  
errors.add :state\_id, “isn’t valid”  
end  
end  
end

If it’s floating data, you can either attach it to a model:

class User \< ActiveRecord::Base  
attr\_accessor :state\_id #not in the database, but connected with  
the User model  
end

or you can handle it separately by using the flash, as you suggested.

Duane J.  
(canadaduane)  
[http://blog.inquirylabs.com/](http://blog.inquirylabs.com/)

---

<div class="post-metadata">

**Author:** ![nekapoor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ea666f/32.png) [@nekapoor](https://www.ruby-forum.com/u/nekapoor)\
**Post date:** [January 11, 2006, 7:46pm UTC](https://www.ruby-forum.com/t/how-to-display-the-error-messages-from-the-controller/52170/3 "2006-01-11T19:46:12Z")

</div>

Thanks Duane.

As I mentioned it’s a wizard. The state\_id is related to the database  
but it  
will be saved only when the rest of the data comes from the page2.

One the page1, the user has to choose a state that’s it and hit enter. I  
need to verify if a state has been selected or not. I could use flash  
here  
but then it’s difficult to get the same look-n-feel. For example:

1. Displaying messages like (one error prevented the submission of  
form).
2. A state must be selected.

Having the same look-n-feel is difficult with flash and hence I was  
looking  
for an easier way.

-=- Neeraj

---

<div class="post-metadata">

**Author:** ![nekapoor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ea666f/32.png) [@nekapoor](https://www.ruby-forum.com/u/nekapoor)\
**Post date:** [January 11, 2006, 7:46pm UTC](https://www.ruby-forum.com/t/how-to-display-the-error-messages-from-the-controller/52170/4 "2006-01-11T19:46:14Z")

</div>

On 1/11/06, Duane J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > def new  
> > in the model. I could use flash[:notice] but using errors would
> 
> e.g.
> 
> class User \< ActiveRecord::Base  
> def validate  
> if state\_id == -1  
> errors.add :state\_id, “isn’t valid”  
> end  
> end  
> end

If you override the validate method, does all the other automagical  
validation in the model go away, or does this just add to it?

Thanks,

- Ian

---

<div class="post-metadata">

**Author:** ![nekapoor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ea666f/32.png) [@nekapoor](https://www.ruby-forum.com/u/nekapoor)\
**Post date:** [January 11, 2006, 7:52pm UTC](https://www.ruby-forum.com/t/how-to-display-the-error-messages-from-the-controller/52170/5 "2006-01-11T19:52:30Z")

</div>

I haven’t tried it yet. Will give it a shot tonight.

The problem is that the model will have validations for page2 too and I  
don’t want page2 stuff to be validated while performing the operation  
for  
page1.

I guess one solution could be to use a dummy model just for page1. Not  
very  
appealing but it might work and each alternative has it’s own kinks.

-=- Neeraj

---

<div class="post-metadata">

**Author:** ![nekapoor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ea666f/32.png) [@nekapoor](https://www.ruby-forum.com/u/nekapoor)\
**Post date:** [January 11, 2006, 9:14pm UTC](https://www.ruby-forum.com/t/how-to-display-the-error-messages-from-the-controller/52170/6 "2006-01-11T21:14:32Z")

</div>

On Jan 11, 2006, at 11:45 AM, Ian H. wrote:

> > ```
> > end
> > 
> > ```
> > 
> > end  
> > end
> 
> If you override the validate method, does all the other automagical  
> validation in the model go away, or does this just add to it?

Adds to it.

Duane J.  
(canadaduane)  
[http://blog.inquirylabs.com/](http://blog.inquirylabs.com/)

---

<div class="post-metadata">

**Author:** ![nekapoor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ea666f/32.png) [@nekapoor](https://www.ruby-forum.com/u/nekapoor)\
**Post date:** [January 11, 2006, 9:20pm UTC](https://www.ruby-forum.com/t/how-to-display-the-error-messages-from-the-controller/52170/7 "2006-01-11T21:20:36Z")

</div>

On Jan 11, 2006, at 11:51 AM, Neeraj K. wrote:

> -=- Neeraj  
> You can include variables in your model that you use to determine its  
> state. Later, you can check those variables in order to know what  
> should be validated.

For example:

class User \< ActiveRecord::Base  
attr\_writer :steps\_completed

def steps\_completed  
@steps\_completed || 0  
end

def validate  
if steps\_completed \> 0  
# … validate step 1 …  
end

```
 if steps_completed > 1
   # ... validate step 2 ...
 end

```

end  
end

You can also use conditional validations if you’re using the shortcut  
validators:

class User \< ActiveRecord::Base  
attr\_writer :steps\_completed

def steps\_completed  
@steps\_completed || 0  
end

def after\_step\_one  
@steps\_completed \> 1  
end

validates\_presence\_of :state\_id, :if =\> :after\_step\_one  
end

Duane J.  
(canadaduane)  
[http://blog.inquirylabs.com/](http://blog.inquirylabs.com/)
