exception_handler
exception_handler copied to clipboard
Undefined method `layout'
Following all steps closely: I only can see the following message:
500 Internal Server Error If you are the administrator of this website, then please read this web application's log file and/or the web server's log file to find out what went wrong.
Ruby terminal gives me this error:
Error during failsafe response: undefined method
layout' for ExceptionHandler::ExceptionsController:Class
/var/www/sdapi/app/controllers/exception_handler/exceptions_controller.rb:36:in <class:ExceptionsController>' /var/www/sdapi/app/controllers/exception_handler/exceptions_controller.rb:2:in
module:ExceptionHandler'
/var/www/sdapi/app/controllers/exception_handler/exceptions_controller.rb:1:in <top (required)>'
Good morning and thanks for the comment.
It looks like you're using a custom version of the controller (IE it's in your own app) - do you have any references I could look at regarding the code you're using for this?
Thanks for your quick response. Yes, seems we are using
`module V1 class BaseApiController < ApplicationController include DeviseTokenAuth::Concerns::SetUserByToken
then other controllers using the following
class TestController < BaseApiController
`
Hmm - can you show me the code for BaseApiController?
Here it is, but I don't know if that's the issue:
module V1
class BaseApiController < ApplicationController
include DeviseTokenAuth::Concerns::SetUserByToken
before_action :establish_resource, only: %I[destroy show update]
respond_to :json
def create
establish_resource(resource_class.new(resource_params))
if retrieve_resource.save
render :show, status: :created
else
render json: retrieve_resource.errors, status: :unprocessable_entity
end
end
def destroy
retrieve_resource.destroy
head :no_content
end
def index
plural_resource_name = "@#{resource_name.pluralize}"
resources = resource_class.where(query_params)
.page(page_params[:page])
.per(page_params[:page_size])
instance_variable_set(plural_resource_name, resources)
respond_with instance_variable_get(plural_resource_name).all
end
def show
respond_with retrieve_resource
end
def update
if retrieve_resource.update(resource_params)
render :show, status: :updated
else
render json: retrieve_resource.errors, status: :unprocessable_entity
end
end
private
def retrieve_resource
instance_variable_get("@#{resource_name}")
end
def query_params
{}
end
def page_params
params.permit(:page, :page_size)
end
def resource_name
@resource_name ||= controller_name.singularize
end
def resource_params
@resource_params ||= send("#{resource_name}_params")
end
def establish_resource(resource = nil)
resource ||= resource_class.find(params[:id]) if resource_class.exists?(params[:id])
resource ||= resource_class.friendly.find(params[:slug]) if resource_class.exists?(params[:slug])
instance_variable_set("@#{resource_name}", resource)
end
end
end
Update: As long as comment it out "layout :layout" in exception_controller.rb file, the error message will use the locales/en.yml message instead and there will be no "Undefined method `layout'" controller message anymore