Forum: Ruby on Rails Wrap error_messages_for() call when no instance var present

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Bb4bdf2b184027bc38d4fb529770cde5?d=identicon&s=25 Wes Gamble (weyus)
on 2006-03-20 23:27
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
A332dcb1e051f733dc9a9bd78df3b560?d=identicon&s=25 Wes Garrison (Guest)
on 2006-03-20 23:35
(Received via mailing list)
<%= error_messages_for 'target_list' unless @target_list.nil? %>

  -- Wes

On 3/20/06, Wes Gamble <weyus@att.net> wrote:
> <% if @target_list? %>
> Wes
>
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>



--

    -- Wes
Bb4bdf2b184027bc38d4fb529770cde5?d=identicon&s=25 Wes Gamble (weyus)
on 2006-03-21 00:01
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?

<frustration>
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.
</frustration>

Thanks for any insight,
Wes
Wes Garrison wrote:
> <%= error_messages_for 'target_list' unless @target_list.nil? %>
>
>   -- Wes
>
> On 3/20/06, Wes Gamble <weyus@att.net> wrote:
>> <% if @target_list? %>
>> Wes
>>
>> --
>> Posted via http://www.ruby-forum.com/.
>> _______________________________________________
>> Rails mailing list
>> Rails@lists.rubyonrails.org
>> http://lists.rubyonrails.org/mailman/listinfo/rails
>>
>
>
>
> --
>
>     -- Wes
A332dcb1e051f733dc9a9bd78df3b560?d=identicon&s=25 Wes Garrison (Guest)
on 2006-03-21 00:22
(Received via mailing list)
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 Gamble <weyus@att.net> 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
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>



--

    -- Wes
Bb4bdf2b184027bc38d4fb529770cde5?d=identicon&s=25 Wes Gamble (weyus)
on 2006-03-21 00:26
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 Gamble <weyus@att.net> 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
>> Rails@lists.rubyonrails.org
>> http://lists.rubyonrails.org/mailman/listinfo/rails
>>
>
>
>
> --
>
>     -- Wes
A332dcb1e051f733dc9a9bd78df3b560?d=identicon&s=25 Wes Garrison (Guest)
on 2006-03-21 16:50
(Received via mailing list)
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
This topic is locked and can not be replied to.