exception_handler icon indicating copy to clipboard operation
exception_handler copied to clipboard

Undefined method `layout'

Open neodcalgary opened this issue 4 years ago • 5 comments

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)>'

neodcalgary avatar Feb 06 '20 21:02 neodcalgary

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?

richpeck avatar Feb 07 '20 11:02 richpeck

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

`

neodcalgary avatar Feb 10 '20 16:02 neodcalgary

Hmm - can you show me the code for BaseApiController?

richpeck avatar Feb 10 '20 16:02 richpeck

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

neodcalgary avatar Feb 10 '20 16:02 neodcalgary

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

neodcalgary avatar Feb 12 '20 19:02 neodcalgary