Undefined method `model_name 'for ActiveModel :: Errors: class

I have the following mongoid model class:

class Exercise include Mongoid::Document field :name, :type => String field :description, :type => String belongs_to :group validates_presence_of :name, :description, :group end 

And I have the following controller:

 class ExercisesController < ApplicationController respond_to :json def create @exercise = Exercise.create(params[:exercise]) if @exercise.save respond_with @exercise else respond_with(@exercise.errors, :status => :unprocessable_entity) end end end 

The model retains the penalty when it is valid, but when the following line is executed:

 respond_with(@exercise.errors, :status => :unprocessable_entity) 

I get the following error

undefined method `` model_name '' for ActiveModel :: Errors: Class

The error collection is full, so I think the response_with syntax is incorrect.

+4
source share
1 answer

Rails response_with helper expects to receive rail model objects as the 1st parameter. So in this case you just need answer_with @exercise, status :: unprocessable_entity And then in your answer you will need to format the error data correctly, I assume that you are doing this through ajax and are responding to json, etc. Hope this helps.

+2
source

All Articles