0

我已经定义了我的ApplicationController以下内容:

class ApplicationController < ActionController::Base

  before_action :set_shop

  attr_reader :current_shop

  protected

  def set_shop
    if params[:shop].present?
      @current_shop ||= Shop.find_by(domain: params[:shop])
    else
      @current_shop ||= Shop.find_by(domain: request.headers['HTTP_X_SHOPIFY_SHOP_DOMAIN'])
    end
  end

end

另一个继承自的控制器ApplicationController

class V1::CcsController < ApplicationController

  def test_ccs
    current_shop
    @current_shop
    byebug
  end

end

我的文件夹结构很好,意思ApplicationController是不在V1文件夹CcsController中。

某些原因可能很简单,我无法访问current_shop. 我确实确保正在设置 current_shop。

那么我怎样才能访问current_shop我的CcsController

4

1 回答 1

1

我正在使用与您相同的结构进行测试,但我没有遇到问题,在我的示例中,我删除了attr_reader :current_shopApplicationController 中的,您可以使用 before_action 中定义的 @current_shop 实例:set_shop

class ApplicationController < ActionController::Base
  before_action :set_shop

  protected

  def set_shop
    @current_shop = 'example'
  end
end

class V1::PublicController < ApplicationController
  def index
    byebug
  end
end

在我的带有调试器的控制台上:

   1: class V1::PublicController < ApplicationController
   2:   def index
   3:     byebug
=> 4:   end
   5: end
(byebug) @current_shop
"example"
于 2021-01-08T01:22:47.260 回答