emersonthis
emersonthis

Reputation: 33408

Rails 4: How to check validation errors from inside a controller

I have some basic model validations that are triggered when a form gets submitted by AJAX. If the validations fail, I want to pass the validation errors back to the view so I can tell the user.

  def save
    logger.debug( params )
    @video = Video.new( video_params )
    if @video.save
        render json: @video
    else
        render json: errors.messages
    end
  end

This throws an error because errors is undefined. What am I doing wrong? I read the docs on this and it only shows errors.messages used in the view.

Upvotes: 2

Views: 7935

Answers (2)

Sandip Mondal
Sandip Mondal

Reputation: 921

instead of messages use full_messages, like below

@video.errors.full_messages

Upvotes: 2

fotanus
fotanus

Reputation: 20126

errors is a instance method from an ActiveRecord. The correct way to use in your case is like this:

@video.errors.messages

Upvotes: 9

Related Questions