In my controller I have this code in one of my actions:
begin
@user = User.find(params[:id])
@user.destroy
rescue ActiveRecord::RecordNotFound
render :json => {"status" => "404", "message" => "User with id #{params[:id]} not found"}
return
end
And is working fine, but I dont want to copy paste it to all the methods which require to run a Select query.
So I found this answer How to redirect to a 404 in Rails?
And then tried slightly different since I am rendering JSON API endpoints instead templates. Note also I dont know if that params[:id] will be defined there.
def not_found
render :json => {"status" => "404", "message" => "User with id #{params[:id]} not found"}
end
Anyway I modified the query with:
@user = User.find(params[:id]) or not_found
But is still raising the ActiveRecord::RecordNotFound exception.
Also would it be possible to create a generic not_found action which I can use in all the controllers which I can pass it the id parameter and the type of the Object?
Like some generic 404, 500, 400, 200 methods which render JSON responses where I can just pass some parameters