Reputation: 28790
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 saves fine when valid but when the following line is ran:
respond_with(@exercise.errors, :status => :unprocessable_entity)
I get the following error
undefined method `model_name' for ActiveModel::Errors:Class
The errors collection is populated so I think my respond_with syntax is wrong.
Upvotes: 4
Views: 1373
Reputation:
The rails respond_with helper expects to receive a rails model objects as the 1st parameter. So in this case you'd just want respond_with @exercise, status: :unprocessable_entity And then in your response view you would need to properly format the error data, I'm assuming you are doing this via ajax and responding with json, etc. Hope that helps.
Upvotes: 2