# Wrap error\_messages\_for() call when no instance var present

**URL:** <https://www.ruby-forum.com/t/wrap-error-messages-for-call-when-no-instance-var-present/84405>\
**Category:** Rails\
**Created:** [March 20, 2006, 11:27pm UTC](https://www.ruby-forum.com/t/wrap-error-messages-for-call-when-no-instance-var-present/84405 "2006-03-20T23:27:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [March 20, 2006, 11:27pm UTC](https://www.ruby-forum.com/t/wrap-error-messages-for-call-when-no-instance-var-present/84405/1 "2006-03-20T23:27:30Z")

</div>

I am trying to put some smarts around a call to

\<%= error\_messages\_for %\>

so that when I first come into this page i.e. when my instance variable  
@target\_list has not been defined yet, the page doesn’t break.

I’m doing this:

\<% if @target\_list? %\>  
\<%= error\_messages\_for ‘target\_list’ %\>  
\<% end %\>

I get a syntax error on line 1. What is wrong with

if @target\_list?

to test for the existence of the @target\_list variable?

Thanks,  
Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [March 20, 2006, 11:35pm UTC](https://www.ruby-forum.com/t/wrap-error-messages-for-call-when-no-instance-var-present/84405/2 "2006-03-20T23:35:04Z")

</div>

\<%= error\_messages\_for ‘target\_list’ unless @target\_list.nil? %\>

– Wes

On 3/20/06, Wes G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> \<% if @target\_list? %\>  
> Wes
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–

```
-- Wes

```

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [March 21, 2006, 12:01am UTC](https://www.ruby-forum.com/t/wrap-error-messages-for-call-when-no-instance-var-present/84405/3 "2006-03-21T00:01:01Z")

</div>

Wes,

Thanks!

That gets around the original problem of the initial display but the  
error\_messages\_for method doesn’t appear to be able to handle the  
instance variable target\_list.

The controller method [let’s call it x] that my page posts to has the  
following code

def x

@target\_list = TargetList.create()  
@target\_list.name = name  
@target\_list.save!

…

and my model for TargetList has

:validates\_presence\_of :name

So, if I call x based on the post from my form which includes

\<%= error\_messages\_for ‘target\_list’ unless @target\_list.nil? %\>

I get an ugly “Record Invalid” message when I call save! instead of the  
error\_messages\_for output.

Does this not work because the initial definition of the @target\_list  
instance variable in the controller is within method x?

I wish I could get helper methods to actually work in non-trivial cases. I'm not trying to use a scaffold to do my model managmeent and it seems to me that \_because\_ I'm not using a scaffold, I can't take advantage of any of these helper things.

Thanks for any insight,  
Wes  
Wes Garrison wrote:

> \<%= error\_messages\_for ‘target\_list’ unless @target\_list.nil? %\>
> 
> – Wes
> 
> On 3/20/06, Wes G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > \<% if @target\_list? %\>  
> > Wes
> > 
> > –  
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> > 
> > * * *
> > 
> > Rails mailing list  
> > removed\_email\_address@domain.invalid  
> > [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)
> 
> –
> 
> ```
> -- Wes
> 
> ```

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [March 21, 2006, 12:22am UTC](https://www.ruby-forum.com/t/wrap-error-messages-for-call-when-no-instance-var-present/84405/4 "2006-03-21T00:22:39Z")

</div>

I don’t really use that helper. With that warning in place…

First, try:  
@target\_list.save  
… without the ! that forces it to save in place.

Normally, I do this:  
if @target\_list.save  
flash[‘notice’] = “Yay!”  
redirect\_to :action =\> ‘list’  
else  
flash[‘error’] = “Nay?!”  
render :action =\> ‘new’  
end

Someone else feel free to chime in with “error\_messages\_for” help…

– Wes

PS: Stop taking all the “wesg” login names everywhere.

On 3/20/06, Wes G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> following code
> 
> instance variable in the controller is within method x?  
> Wes Garrison wrote:
> 
> > > * * *
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–

```
-- Wes

```

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [March 21, 2006, 12:26am UTC](https://www.ruby-forum.com/t/wrap-error-messages-for-call-when-no-instance-var-present/84405/5 "2006-03-21T00:26:32Z")

</div>

My logins are all wgamble so I don’t wanna hear any crap :)…

Thanks for the advice.

I just need to understand how all this magic works - I can’t just accept  
it.

That will be my ultimate problem in adopting Rails. I’m ok with magic  
if it’s consistent and there are clear rules around it.

But the magic here _appears_ to be highly context-specific and it’s  
starting to get on my nerves :).

On the other hand, I really love ActiveRecord so far. It’s the view  
stuff (that I am terrible at to begin with) that is really getting my  
goat…

WG

Wes Garrison wrote:

> I don’t really use that helper. With that warning in place…
> 
> First, try:  
> @target\_list.save  
> … without the ! that forces it to save in place.
> 
> Normally, I do this:  
> if @target\_list.save  
> flash[‘notice’] = “Yay!”  
> redirect\_to :action =\> ‘list’  
> else  
> flash[‘error’] = “Nay?!”  
> render :action =\> ‘new’  
> end
> 
> Someone else feel free to chime in with “error\_messages\_for” help…
> 
> – Wes
> 
> PS: Stop taking all the “wesg” login names everywhere.
> 
> On 3/20/06, Wes G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > following code
> > 
> > instance variable in the controller is within method x?  
> > Wes Garrison wrote:
> > 
> > > > * * *
> > 
> > –  
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> > 
> > * * *
> > 
> > Rails mailing list  
> > removed\_email\_address@domain.invalid  
> > [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)
> 
> –
> 
> ```
> -- Wes
> 
> ```

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [March 21, 2006, 4:50pm UTC](https://www.ruby-forum.com/t/wrap-error-messages-for-call-when-no-instance-var-present/84405/6 "2006-03-21T16:50:54Z")

</div>

If you haven’t yet, take a look at the api [http://api.rubyonrails.com](http://api.rubyonrails.com)  
for  
ActiveRecord::Validations

It’s got an explanation of the Errors object there and how it gets  
loaded on  
validation.

When I have a behind-the-scenes question, I use the API and read the  
source  
for the functions I’m interested in. That really helps me to know  
what’s  
actually happening, plus I get to see cool Ruby tricks that I didn’t  
know  
otherwise.

– The Real WG
