a dumb question?

Subscribe to a dumb question? 3 post(s), 2 voice(s)

 
Avatar stranger 46 post(s)

Hi Eldon, I want to ask a dumb question? How to use error_messages_for exactly.
Session_controller.rb – create method

else
      render :action => 'new'
      flash[:notice] = "Invalid username/password combination" 
    end   

new.rhtml
<%= error_messages_for :create %>

<% form_tag session_path do -%> 

I am not able to see the error message. Could you exactly tell me what paramaters should match. that means what should be in error_messages_for :value and from where that value comes from…

 
Avatar EldonAlameda Administrator 216 post(s)

No dumb questions.

I was trying to explain in the previous section that this page really isn’t designed for using error_messages_for—that’s why I recommended using the flash instead. So to display the flash message you’re creating in the controller you would just need to replace the error_messages_for call with a simple

<%= flash[:notice] %>

As for the error_messages_for—Take a look on page 145-146 and you can see how the error_messages_for method is being used in the scaffold created templates for a goal in the edit template. There the edit controller has created a goal object (@goal). It’s errors within this goal object that would cause this method to display anything.

Which might make more sense if I demo it in script/console

ruby script/console 
Loading development environment.
>> e = Excercise.new
=> #<Excercise:0x21f17cc @attributes={"name"=>nil, "user_id"=>nil}, @new_record=true>

So here we’ve started up a script/console session and created a new exercise object (stored in variable e). However the Excercise model has a few validation requirements on it such as “validates_presence of :name”—so if we haven’t set a name and attempt to save the object – we get:
>> e.save
=> false

but now let’s take a look at at our excercise object and see how it’s changed
>> e
=> #<Excercise:0x21f17cc @attributes={"name"=>nil, "user_id"=>nil}, @new_record=true, 
@errors=#<ActiveRecord::Errors:0x24e657c @errors={"name"=>["can't be blank"]}, 
@base=#<Excercise:0x21f17cc ...>>>

See those values stored in @errors – thats what the error_messages_for method is looking for. The login method isn’t creating any instance variables of an activerecord object that would be used on these pages – so the error_messages_for method has nothing to display.

Does that help?

 
Avatar stranger 46 post(s)

got the point…. thanks…. It think it took more than 5minutes to type all that… thank you