Wes Gamble
2006-Mar-20 22:27 UTC
[Rails] Wrap error_messages_for() call when no instance var present
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 -- Posted via http://www.ruby-forum.com/.
Wes Garrison
2006-Mar-20 22:34 UTC
[Rails] Wrap error_messages_for() call when no instance var present
<%= error_messages_for ''target_list'' unless @target_list.nil? %> -- Wes On 3/20/06, Wes Gamble <weyus@att.net> wrote:> > 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 > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- -- Wes -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060320/4010edc9/attachment-0001.html
Wes Gamble
2006-Mar-20 23:01 UTC
[Rails] Re: Wrap error_messages_for() call when no instance var pres
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-- Posted via http://www.ruby-forum.com/.
Wes Garrison
2006-Mar-20 23:22 UTC
[Rails] Re: Wrap error_messages_for() call when no instance var pres
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:> > 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 > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- -- Wes -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060320/7cc851f9/attachment.html
Wes Gamble
2006-Mar-20 23:26 UTC
[Rails] Re: Re: Wrap error_messages_for() call when no instance var
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-- Posted via http://www.ruby-forum.com/.
Wes Garrison
2006-Mar-21 15:50 UTC
[Rails] Re: Re: Wrap error_messages_for() call when no instance var
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 On 3/20/06, Wes Gamble <weyus@att.net> wrote:> > 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... >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060321/868c3d21/attachment.html
Seemingly Similar Threads
- Using <%= text_field %> within partials is problematic
- checkboxes with a has_many :through relation
- What does the send() method do on a model object?
- belongs_to <parent name>, :foreign_key modifier not working
- text_field doesn''t call overridden ActiveRecord getters