Rails 中未定义的 ApplicationController 辅助方法
ApplicationController helper method undefined in Rails
我正在尝试编写一个简单的辅助方法来检查用户的电子邮件地址是否包含特定域名。该方法在我的所有控制器中都需要,并且取决于 current_user
的状态,因此我觉得放置它的最佳位置是在我的 ApplicationController 中。根据第 3 行方法的评估,调用第二个辅助方法。我在 this question's answer.
上模拟了我的设置
当我插入这段代码并刷新页面时,应该会点击binding.pry
,这表明正在评估我定义的辅助方法,但这并没有发生。相反,:disable_intercom?
的计算结果为 true,而没有使用下面的方法。如果我删除冒号,我会收到一条错误消息,指出 disable_intercom?
未定义。我在这里错过了什么?
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
skip_after_action :intercom_rails_auto_include if :disable_intercom?
private
def disable_intercom?
binding.pry
if user_signed_in?
current_user.email.split('@').include?('mysite.com') ? true : false
else
false
end
end
end
谢谢!
更新:
以下是我发现的关于此问题的其他一些 Whosebug 讨论。我已经尝试了提出的各种解决方案,但似乎没有任何东西可以触发 disable_intercom?
方法。
conditionally apply skip_before_filter with :if => condition in rails 4
skip_before_filter ignores conditionals
替换:
skip_after_action :intercom_rails_auto_include if :disable_intercom?
与:
skip_after_action :intercom_rails_auto_include, if: :disable_intercom?
在你的初始代码中,这是有效的 ruby,你基本上总是跳过,因为你没有将 if
作为选项传递给 skip_after_action
,当符号是真实的,即总是
看来问题是skip_after_action
不接受条件参数,例如if:
或unless:
。即使您传递了条件,skip_after_action
回调也会始终执行。
对我来说不幸的是,这意味着无法用代码做我想做的事情。我正在研究其他有条件地禁用对讲机 API 的方法,因为使用有条件的 skip_after
回调不是一个可行的解决方案。
我正在尝试编写一个简单的辅助方法来检查用户的电子邮件地址是否包含特定域名。该方法在我的所有控制器中都需要,并且取决于 current_user
的状态,因此我觉得放置它的最佳位置是在我的 ApplicationController 中。根据第 3 行方法的评估,调用第二个辅助方法。我在 this question's answer.
当我插入这段代码并刷新页面时,应该会点击binding.pry
,这表明正在评估我定义的辅助方法,但这并没有发生。相反,:disable_intercom?
的计算结果为 true,而没有使用下面的方法。如果我删除冒号,我会收到一条错误消息,指出 disable_intercom?
未定义。我在这里错过了什么?
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
skip_after_action :intercom_rails_auto_include if :disable_intercom?
private
def disable_intercom?
binding.pry
if user_signed_in?
current_user.email.split('@').include?('mysite.com') ? true : false
else
false
end
end
end
谢谢!
更新:
以下是我发现的关于此问题的其他一些 Whosebug 讨论。我已经尝试了提出的各种解决方案,但似乎没有任何东西可以触发 disable_intercom?
方法。
conditionally apply skip_before_filter with :if => condition in rails 4
skip_before_filter ignores conditionals
替换:
skip_after_action :intercom_rails_auto_include if :disable_intercom?
与:
skip_after_action :intercom_rails_auto_include, if: :disable_intercom?
在你的初始代码中,这是有效的 ruby,你基本上总是跳过,因为你没有将 if
作为选项传递给 skip_after_action
,当符号是真实的,即总是
看来问题是skip_after_action
不接受条件参数,例如if:
或unless:
。即使您传递了条件,skip_after_action
回调也会始终执行。
对我来说不幸的是,这意味着无法用代码做我想做的事情。我正在研究其他有条件地禁用对讲机 API 的方法,因为使用有条件的 skip_after
回调不是一个可行的解决方案。