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
Ruby on rails 如何访问模型中的当前用户对象?_Ruby On Rails_Ruby_Ruby On Rails 3_Ruby On Rails 4_Devise - Fatal编程技术网

Ruby on rails 如何访问模型中的当前用户对象?

Ruby on rails 如何访问模型中的当前用户对象?,ruby-on-rails,ruby,ruby-on-rails-3,ruby-on-rails-4,devise,Ruby On Rails,Ruby,Ruby On Rails 3,Ruby On Rails 4,Devise,我试图在我的“团队”模型中编写一个方法,但是当前的用户显示了这个错误 未定义的局部变量或方法“当前用户”# 当前用户使用的方法对于其他模型和控制器工作良好。我这样称呼这个方法 has_attached_file :logo, :styles => { :medium => "200x200#", :thumb => "100x100#", :small => "50x50#" }, :default_url => :set_default_ur

我试图在我的“团队”模型中编写一个方法,但是当前的用户显示了这个错误

未定义的局部变量或方法“当前用户”#

当前用户使用的方法对于其他模型和控制器工作良好。我这样称呼这个方法

has_attached_file :logo, :styles => { 
  :medium => "200x200#", 
  :thumb => "100x100#", 
  :small => "50x50#" 
}, 
:default_url => :set_default_url

我正在使用rails 3.2、ruby 1.9.3和Desive 3.1。这似乎是一项简单的任务,但我不明白错在哪里。如果有人在这里帮助我,我将非常感激。

任何型号中都没有当前用户
,要访问型号中的当前用户,请执行此操作

在应用程序控制器中

before_filter :set_current_user

def set_current_user
  Team.current_user = current_user
end
before_filter :set_current_user

def set_current_user
  User.current_user = current_user
end
在您的
团队中
模型中添加此行

cattr_accessor :current_user
恭喜,现在每个型号都有
current_user
可用,要获得当前用户,只需在任何地方使用以下行即可

Team.current_user
注意:添加上述行后重新启动服务器

现在在你的问题中,你可以像这样使用它

def set_default_url
  if Team.current_user.id == self.user_id
    "/assets/default_black_:style_logo.jpg"
  else
    "/assets/default_:style_logo.jpg"
  end
end

希望这有帮助

如果只在调用此方法时使用它一次,请将当前用户作为参数传递,如下所示

has_attached_file :logo, :styles => { 
  :medium => "200x200#", 
  :thumb => "100x100#", 
  :small => "50x50#" 
}, 
:default_url => :set_default_url(current_user)
在模型中

def set_default_url(current_user)
  if current_user.id == self.user_id
    "/assets/default_black_:style_logo.jpg"
  else
    "/assets/default_:style_logo.jpg"
  end
end
如果您不想执行上述步骤,请遵循以下步骤

转到用户模型

def self.current_user
  Thread.current[:user]
end

def self.current_user=(user)
  Thread.current[:user] = user
end
然后转到应用程序控制器

before_filter :set_current_user

def set_current_user
  Team.current_user = current_user
end
before_filter :set_current_user

def set_current_user
  User.current_user = current_user
end
现在,我们可以轻松地获取任何模型中的当前用户,而不仅仅是团队中的用户

只需在代码中指定as User.current_User so

def set_default_url
  if User.current_user.id == self.user_id
    "/assets/default_black_:style_logo.jpg"
  else
    "/assets/default_:style_logo.jpg"
  end
end
所以好好利用它吧

希望它能很好地解决您的问题。可在任何型号中免费使用

User.current_User获取当前用户 User.current\u User=分配当前用户


谢谢

您可能想看看这个,您是否设置了
用户
模型与
团队
模型的任何关联?@GaganGami是的!当然了。当前用户返回nil,即使我登录操作,很抱歉,我错过了一行,在应用程序控制器中,在\u过滤器之前添加此行
:设置\u当前用户
,我已经编辑了答案