Wrap error_messages_for() call when no instance var present


#1

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


#2

<%= error_messages_for ‘target_list’ unless @target_list.nil? %>

– Wes

On 3/20/06, Wes G. removed_email_address@domain.invalid wrote:

<% if @target_list? %>
Wes


Posted via http://www.ruby-forum.com/.


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails

-- Wes

#3

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 wrote:

<% if @target_list? %>
Wes


Posted via http://www.ruby-forum.com/.


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails

-- Wes

#4

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 wrote:

following code

instance variable in the controller is within method x?
Wes Garrison wrote:



Posted via http://www.ruby-forum.com/.


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails

-- Wes

#5

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 wrote:

following code

instance variable in the controller is within method x?
Wes Garrison wrote:



Posted via http://www.ruby-forum.com/.


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails

-- Wes

#6

If you haven’t yet, take a look at the api 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