Re: nil.errors and error_messages_for

Phlip
Thu, 26 Mar 2009 04:36:50 -0000

bongoman wrote:

> I''m wanting to re-render my index action in the event of an object
not
> getting saved; I use render not redirect so as to preserve the
> validation errors.
>
> I''m using the error_messages_for helper in my view but I get a
> nil.errors error when I attempt to display the view.
>
> I can avoid this error by setting up @discount = Discount.new in my
> index action, but this obliterates the instance variable with the
> attached error messages.
>
> I''m chasing my tail on this and any clues would be appreciated.

Hear galloping think horses not zebras, here...

Your action goes like...

  @discount = Discount.new(params[:discount]) # warning use attr_protected!
  unless @discount.save
    render :action => ''index''
  end

So... uh... @discount indeed contains a stuffed Discount instance with populated
errors... so what''s the problem??

So post your actual action instead of making us guess? (-8