Ruby on rails Can';t访问视图中的应用程序帮助程序方法
我有一个检查表上布尔值的方法,它在我的用户视图中返回false(在其他任何地方都返回true)。我很难理解为什么它是可访问的,但返回false 我的代码: 应用程序控制器:Ruby on rails Can';t访问视图中的应用程序帮助程序方法,ruby-on-rails,ruby,ruby-on-rails-4,model-view-controller,Ruby On Rails,Ruby,Ruby On Rails 4,Model View Controller,我有一个检查表上布尔值的方法,它在我的用户视图中返回false(在其他任何地方都返回true)。我很难理解为什么它是可访问的,但返回false 我的代码: 应用程序控制器: protected def current_user @current_user ||= User.find_by_id(session_user_id) if session_user_id end helper_method :current_user def current_customer if cu
protected
def current_user
@current_user ||= User.find_by_id(session_user_id) if session_user_id
end
helper_method :current_user
def current_customer
if current_user.customer and current_user.customer.master?
else
current_user.customer
end
end
helper_method :current_customer
应用程序助手方法:
def customer_helper?
(current_user && current_user.customer.boolean_value?)
end
顾客是你的助手吗?方法在所有my Users视图中返回false,即使my UsersController继承自ApplicationController
如果您有任何建议或帮助,我们将不胜感激。
当前用户在helper方法中为nil
。我假设您是在一个视图中访问它的,其中当前用户已经从控制器暴露到该视图中。向视图公开的属性必须传递到helper方法中;它们不会自动在那里提供
如果我的假设是正确的,那么你的助手方法应该是这样的
def customer_helper?(current_user)
(current_user && current_user.customer.boolean_value?)
end
然后,您需要更新视图中对customer\u helper?
的调用,以传入当前用户。在应用程序\u helper中定义的customer\u helper?
方法在哪里?rbcurrent\u customer
将返回nil
如果当前用户.customer&¤t\u用户.customer.master?
计算为对。我想这就是你的问题。我刚刚使用binding.pry,你是对的,对于nil:NilClass from(pry):27:in
boolean\u value?,这是否意味着当前的用户.customer.master?无法访问布尔值列?请解释您的意图,因为可能有更简单的方法来处理此问题。现在您的错误是当前用户
为零,因此没有客户
方法。我指出您没有返回current\u customer
,但我认为您需要重新查看代码,以确定current\u user
为什么是nil
。