Ruby on rails attr_reader不允许我访问其他控制器中的变量
我已在下面定义了我的Ruby on rails attr_reader不允许我访问其他控制器中的变量,ruby-on-rails,ruby,Ruby On Rails,Ruby,我已在下面定义了我的应用程序控制器: 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])
应用程序控制器
:
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
不在V1
文件夹中,而ccscocontroller
在
原因很简单,我无法访问当前的商店。我确实确定了当前的_商店正在设置中
那么我如何在我的
ccscocontroller
中访问current\u shop
?我正在使用与您相同的结构进行测试,我没有遇到问题,在我的示例中,我删除了ApplicationController中的attr\u reader:current\u shop
,您可以使用在before\u action:set\u shop>中定义的@current\u shop实例
class ApplicationController < ActionController::Base
before_action :set_shop
protected
def set_shop
@current_shop = 'example'
end
end
class ApplicationController
class V1::PublicController
在带有调试器的我的控制台上:
1: class V1::PublicController < ApplicationController
2: def index
3: byebug
=> 4: end
5: end
(byebug) @current_shop
"example"
1:class V1::PublicController4:结束
5:完
(byebug)@current_店
“示例”
你能解决这个问题吗?@JuanseGimenez是的,我是个白痴!!这是if的声明。。与其他事情无关。。我没有在if语句之外定义变量。所以它没有返回任何东西。。谢谢你的帮助!不是真正的答案,这是我的if声明,但我将此标记为努力的答案!谢谢
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"