Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/53.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Can';t访问视图中的应用程序帮助程序方法_Ruby On Rails_Ruby_Ruby On Rails 4_Model View Controller - Fatal编程技术网

Ruby on rails Can';t访问视图中的应用程序帮助程序方法

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

我有一个检查表上布尔值的方法,它在我的用户视图中返回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 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?
方法在哪里?rb
current\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