N
N
Nikita2015-06-07 20:16:39
Ruby on Rails
Nikita, 2015-06-07 20:16:39

How to correctly translate the ActiveRecord::RecordNotFound exception?

There is such an exception interceptor:

rescue_from ActiveRecord::RecordNotFound do |exception|
    render json: {
      error: exception.message
    }, status: 404
end

When there is no record with the required id in the database, ActiveRecord returns the following message
Couldn't find Event with id=222
I tried to translate it this way
rescue_from ActiveRecord::RecordNotFound do |exception|
    render json: {
      error: error: I18n.t('activerecord.exceptions.not_found', :class_name => self.class.name.demodulize, :id => params[:id])
    }, status: 404
end

The problem is that self.class returns the EventsController controller, not the model name. And I need the message to be identical. And I'm not sure that it is impossible to do something easier and more correctly.
(the translation will be in German, by the way, you can suggest the correct translation :)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question