Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.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/url/2.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 4 Rails 4:在何处放置所有控制器所需的函数_Ruby On Rails 4_Methods_Controller - Fatal编程技术网

Ruby on rails 4 Rails 4:在何处放置所有控制器所需的函数

Ruby on rails 4 Rails 4:在何处放置所有控制器所需的函数,ruby-on-rails-4,methods,controller,Ruby On Rails 4,Methods,Controller,我试图确保以下方法 def current_user current_user = current_member end 可用于“我的所有控制器”中的所有操作 我尝试过将其放入ApplicationController,但没有成功 我试着在以下几个方面使用解决方案 没有效果 Rails的解决方案是什么 我的ApplicationHelper中有相同的方法,我可以在视图中访问它,这没有问题 编辑: 提供更多细节。 我

我试图确保以下方法

           def current_user 
                current_user = current_member
           end
可用于“我的所有控制器”中的所有操作 我尝试过将其放入ApplicationController,但没有成功

我试着在以下几个方面使用解决方案

没有效果

Rails的解决方案是什么

我的ApplicationHelper中有相同的方法,我可以在视图中访问它,这没有问题

编辑:

提供更多细节。 我有一个应用程序,它带有一个我从头构建的身份验证系统,它使用了一个名为“current_user”的SessionHelper文件中的函数

我一直在我的应用程序中实施Desive,并维护了我的用户模型以保存用户详细信息,但创建了一个成员模型以保存Desive身份验证信息(即,将用户配置文件信息与Desive按照文档建议使用的表分开)

这给了我一个叫做current_member的designe助手方法(基于我对模型的命名)

在控制器操作和视图中,我的应用程序中到处都有“当前用户”

我想创建一个应用程序范围的帮助程序,将当前\u成员别名为当前\u用户。严格地说,在我的问题中,我的函数是错误的-这将把当前用户分配给成员类的实例。由于成员和用户之间存在一对一的关系,外键为member.id,因此正确的函数是

def当前用户 如果成员登录? 当前用户=用户。按成员id查找(当前成员id) 结束 结束

我的应用程序助手:

   module ApplicationHelper


     def current_user
       if member_signed_in?
       current_user = User.find_by_member_id(current_member.id)
      end
    end
  end
这将照顾到所有视图中的当前用户, 但是我无法让它在控制器中工作……例如,请参见我的UserController的“show”操作中的代码

def show
    @associates = []
    @colleagues = current_user.nearbys(1000).take(20)
    @colleagues.each do |associate|
       unless current_user.following?(associate) || current_user == associate
       @associates.push(associate)
    end
   end
    impressionist(@user)
end
忘了逻辑吧——我只是在用geocoder寻找几乎所有的用户。当前_用户正在解析为“nil”

即使我把

  before_action :current_user

    def current_user
       if member_signed_in?
       current_user = User.find_by_member_id(current_member.id)
      end
    end
在UserController中,当前用户未在操作中工作。我的当前用户也在其他控制器的操作中,应用程序在这些点中断,但当当前用户在视图中时不会中断

如果你需要更多信息,请告诉我

编辑2:

我补充说

  before_action :authenticate_member!
但这仍然没有任何效果

编辑3:

我是个白痴。发生nil类错误是因为数据库中没有种子数据,因此

      @colleagues = current_user.nearbys(1000).take(20) 
@同事们都是零,因此对零进行“接受”是一个错误。
新手犯的错误

定义应用程序操作时,如果希望它们在所有其他操作中都可用,则需要设置before筛选器。因此,在您的应用程序控制器中,您将有如下内容:

before_action :current_user

def current_user
    current_user = current_member
end

这将在应用程序中的任何其他操作之前运行此操作,而不考虑控制器

我认为您的问题有两个部分

1-将所有控制器所需的功能放在何处

因此,一般的答案是将它们放在
ApplicationController
中,因为通常所有其他控制器都继承自
ApplicationController

2-关于你所遇到的错误

我的猜测是,在调用
designe
方法之前,您没有加载
designe
。所以试试类似的方法

class ApplicationController < ActionController::Base
   before_action :authenticate_member!
   before_action :current_user

   def current_user
     #your method
   end
end
class ApplicationController
作为一个建议,由于您在helpers中使用相同的方法,为了保持干燥,您可以将controller方法设置为helper方法。因此,它将在所有视图中都可用

class ApplicationController < ActionController::Base
  helper_method :current_user
  before_action :authenticate_member!
  before_action :current_user

  def current_user
    #your method
  end
end
class ApplicationController
因此,在您看来,您可以使用
当前用户

如果所有这些都失败了,正如@abbott567所说的,发布您的错误日志


HTH

您能再显示一些代码吗?在没有看到代码或错误的情况下,很难找到错误所在。。。