Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Undefined method `layout' #89

Open
neodcalgary opened this issue Feb 6, 2020 · 5 comments
Open

Undefined method `layout' #89

neodcalgary opened this issue Feb 6, 2020 · 5 comments

Comments

@neodcalgary
Copy link

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

@richpeck
Copy link
Owner

richpeck commented Feb 7, 2020

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?

@neodcalgary
Copy link
Author

neodcalgary commented Feb 10, 2020

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

`

@richpeck
Copy link
Owner

Hmm - can you show me the code for BaseApiController?

@neodcalgary
Copy link
Author

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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants