简体   繁体   English

如何访问模型中的current_user对象?

[英]How to access current_user object in model?

I am trying to write a method in my "team" model but current_user is showing this error 我试图在我的“团队”模型中编写一个方法,但current_user显示此错误

undefined local variable or method `current_user' for # 未定义的局部变量或方法`current_user'for#

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

The method current_user is working fine for other models and controllers . current_user方法适用于其他模型和控制器。 I am calling this method like this . 我这样称呼这个方法。

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

I am using rails 3.2 , ruby 1.9.3 and devise 3.1 . 我使用rails 3.2,ruby 1.9.3和设计3.1。 It seems to be simple task but I don't understand where the fault lies . 这似乎是一项简单的任务,但我不明白故障所在。 I will be really thankful if someone helps me out here . 如果有人帮助我,我会非常感激。

current_user is not available in any model, to access current_user in model do this current_user在任何模型中都不可用,要访问模型中的current_user ,请执行此操作

In your application controller 在您的应用程序控制器

before_filter :set_current_user

def set_current_user
  Team.current_user = current_user
end

in your Team model add this line 在您的Team模型中添加此行

cattr_accessor :current_user

Congrats, now current_user is available in every model, to get the current user just use the following line everywhere 恭喜,现在current_user在每个型号都可用,以使当前用户在任何地方使用以下行

Team.current_user

NOTE: Restart the server after adding the lines mentioned above! 注意:添加上述行后重新启动服务器!

Now in your question you can use it like 现在在您的问题中,您可以使用它

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

Hope this helps! 希望这可以帮助!

If you are using it only once than while calling this method pass current_user as argument like 如果你只使用它一次,那么在调用此方法时,将current_user作为参数传递

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

and in model 在模型中

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

If you dont want above step then follow these 如果您不想上述步骤,请遵循以下步骤

Go to User Model 转到用户模型

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

def self.current_user=(user)
  Thread.current[:user] = user
end

Then go to application controller 然后转到应用程序控制器

before_filter :set_current_user

def set_current_user
  User.current_user = current_user
end

Now we can easily fetch current_user in any model not only in Team 现在,我们不仅可以在Team中轻松获取任何模型中的current_user

just give as User.current_user so in your code 只需在代码中提供User.current_user即可

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

So make use of it. 所以要利用它。

Hope it solves your issue in a good way. 希望它以一种好的方式解决你的问题。 Free to use in any model 可在任何型号中免费使用

User.current_user to fetch the current user User.current_user= to assign the current user. User.current_user用于获取当前用户User.current_user =以分配当前用户。

Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM