Martin
Martin

Reputation: 11336

Rails errors doesn't work on controllers? how to handle error then?

I have the following action to confirm a PIN code.

def create
  @confirm = User.where(:email => params[:user][:email]).last
  errors[:base] << "Subscription not found" if @confirm.nil?
  respond_with(@confirm)
end

If the object doesn't exist I'm getting the following error

NameError in ConfirmsController#create

undefined local variable or method `errors' for #<ConfirmsController:0x007f921de173d0>

Why it doesn't recognize errors and how can I handle error for this case?

Upvotes: 1

Views: 94

Answers (2)

Renra
Renra

Reputation: 5649

@confirm.errors[:base] << ...

should do the trick

Upvotes: 0

heavysixer
heavysixer

Reputation: 2069

Errors as you are using them are typically on the ActiveRecord model. If you want to display an error message try using the flash method.

Upvotes: 1

Related Questions