Forum: Ruby on Rails problem with error_messages_for

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.
Aa33613fb12765fe505efea87844a12e?d=identicon&s=25 Craig White (Guest)
on 2007-05-24 20:01
(Received via mailing list)
I have a number of validations in client.rb like this...

  validates_length_of :first_name,
    :last_name,
    :maximum => 25,
    :message => "Maximum 25 characters and numbers combined"

and in my clients_controller.rb I have this...

  def update
    @client = Client.find(params[:id])
    if @client.update_attributes(params[:client])
      flash[:notice] = 'Client was successfully updated.'
      redirect_to :back
    else
      flash[:notice] = 'There was a problem with saving your edits.'
      redirect_to :back
    end
  end

and the first line in my view_code is

  <%= error_messages_for 'client' %>

my the flash[:notice] (on my layout) displays properly but nothing ever
displays in the view code.

What detail am I missing? Is it because I am using
':redirect_to :back' ?

--
Craig White <craig@tobyhouse.com>
852a62a28f1de229dc861ce903b07a60?d=identicon&s=25 Gavin Kistner (phrogz)
on 2007-05-24 20:19
(Received via mailing list)
On May 24, 12:00 pm, Craig White <c...@tobyhouse.com> wrote:
> What detail am I missing? Is it because I am using
> ':redirect_to :back' ?

Yes - you need to use render instead to preserve the errors.
Aa33613fb12765fe505efea87844a12e?d=identicon&s=25 Craig White (Guest)
on 2007-05-24 20:53
(Received via mailing list)
On Thu, 2007-05-24 at 11:18 -0700, Phrogz wrote:
> On May 24, 12:00 pm, Craig White <c...@tobyhouse.com> wrote:
> > What detail am I missing? Is it because I am using
> > ':redirect_to :back' ?
>
> Yes - you need to use render instead to preserve the errors.
----
OK - that's why they stopped outputting the 'messages'

I really want to use 'redirect_to' instead of 'render' because I have a
number of different data entry screens using the same 'update' method.

Is it possible to get the same effect in flash[:notice] - I ask because
'error_messages_for' is ActionView and doesn't work in controllers and
something like

flash[:notice] = error_messages_for 'client'
flash[:notice] = @client.errors
flash[:notice] = @client.valid?

all don't work

--
Craig White <craig@tobyhouse.com>
1a9637acaccba514bf6c2cecf80c3f16?d=identicon&s=25 LeonB (Guest)
on 2007-05-30 21:13
(Received via mailing list)
Does anybody has an answer for this? I have the same problem.

And adding a new user while the url is : users/save doesn't seem
"REST" to me.
Af305960da4f6d0530008c8b9d25acd0?d=identicon&s=25 tfwright@gmail.com (Guest)
on 2007-05-30 23:13
(Received via mailing list)
If you need to access formatted errors in controllers your best bet is
to use @object.errors.full_messages, which returns an array of error
messages corresponding to the LIs that error_message_for prints out.
If you are constructing a flash message you might do something like

flash[:warning] = @object.errors.full_messages.join(" ")

Which will yield something like "Attribute X can't be blank. Attribute
Y must be numeric." etc
1a9637acaccba514bf6c2cecf80c3f16?d=identicon&s=25 LeonB (Guest)
on 2007-06-02 16:25
(Received via mailing list)
Beautiful. Gonna try that. Thanks.
But I think Rails won't remember the post values, right?
This topic is locked and can not be replied to.