访问模型中的current_user

访问模型中的current_user

我有3张桌子

items (columns are:  name , type)history(columns are: date, username, item_id)user(username, password)

当用户说“ABC”登录并创建新项目时,将使用以下after_create过滤器创建历史记录。如何通过此过滤器将此用户名“ABC”分配给历史记录表中的用户名字段。

class Item < ActiveRecord::Base
  has_many :histories
  after_create :update_history  def update_history
    histories.create(:date=>Time.now, username=> ?) 
  endend

我在session_controller中的登录方法

def login  if request.post?
    user=User.authenticate(params[:username])
    if user
      session[:user_id] =user.id
      redirect_to( :action=>'home')
      flash[:message] = "Successfully logged in "
    else
      flash[:notice] = "Incorrect user/password combination"
      redirect_to(:action=>"login")
    end
  endend

我没有使用任何身份验证插件。如果有人能告诉我如何在不使用插件(如userstamp等)的情况下实现这一点,我将不胜感激。


慕村9548890
浏览 629回答 3
3回答

POPMUISE

Rails 5声明一个模块module&nbsp;Current &nbsp;&nbsp;thread_mattr_accessor&nbsp;:userend分配当前用户class&nbsp;ApplicationController&nbsp;<&nbsp;ActionController::Base &nbsp;&nbsp;around_action&nbsp;:set_current_user&nbsp;&nbsp;def&nbsp;set_current_user&nbsp;&nbsp;&nbsp;&nbsp;Current.user&nbsp;=&nbsp;current_user&nbsp;&nbsp;&nbsp;&nbsp;yield &nbsp;&nbsp;ensure &nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;to&nbsp;address&nbsp;the&nbsp;thread&nbsp;variable&nbsp;leak&nbsp;issues&nbsp;in&nbsp;Puma/Thin&nbsp;webserver &nbsp;&nbsp;&nbsp;&nbsp;Current.user&nbsp;=&nbsp;nil &nbsp;&nbsp;end&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end现在您可以将当前用户称为&nbsp;Current.user有关thread_mattr_accessor的文档Rails 3,4访问current_user模型内部并不常见。话虽如此,这是一个解决方案:class&nbsp;User&nbsp;<&nbsp;ActiveRecord::Base &nbsp;&nbsp;def&nbsp;self.current&nbsp;&nbsp;&nbsp;&nbsp;Thread.current[:current_user] &nbsp;&nbsp;end &nbsp;&nbsp;def&nbsp;self.current=(usr) &nbsp;&nbsp;&nbsp;&nbsp;Thread.current[:current_user]&nbsp;=&nbsp;usr&nbsp;&nbsp;endendcurrent_user在a&nbsp;around_filter中设置属性ApplicationController。class&nbsp;ApplicationController&nbsp;<&nbsp;ActionController::Base &nbsp;&nbsp;around_filter&nbsp;:set_current_user&nbsp;&nbsp;def&nbsp;set_current_user&nbsp;&nbsp;&nbsp;&nbsp;User.current&nbsp;=&nbsp;User.find_by_id(session[:user_id]) &nbsp;&nbsp;&nbsp;&nbsp;yield &nbsp;&nbsp;ensure &nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;to&nbsp;address&nbsp;the&nbsp;thread&nbsp;variable&nbsp;leak&nbsp;issues&nbsp;in&nbsp;Puma/Thin&nbsp;webserver &nbsp;&nbsp;&nbsp;&nbsp;User.current&nbsp;=&nbsp;nil &nbsp;&nbsp;end&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end设置current_user成功后的身份验证:def&nbsp;login&nbsp;&nbsp;if&nbsp;User.current=User.authenticate(params[:username],&nbsp;params[:password]) &nbsp;&nbsp;&nbsp;&nbsp;session[:user_id]&nbsp;=&nbsp;User.current.id &nbsp;&nbsp;&nbsp;&nbsp;flash[:message]&nbsp;=&nbsp;"Successfully&nbsp;logged&nbsp;in&nbsp;" &nbsp;&nbsp;&nbsp;&nbsp;redirect_to(&nbsp;:action=>'home') &nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;flash[:notice]&nbsp;=&nbsp;"Incorrect&nbsp;user/password&nbsp;combination" &nbsp;&nbsp;&nbsp;&nbsp;redirect_to(:action=>"login") &nbsp;&nbsp;endend最后,指的是current_user在update_history的Item。class&nbsp;Item&nbsp;<&nbsp;ActiveRecord::Base &nbsp;&nbsp;has_many&nbsp;:histories &nbsp;&nbsp;after_create&nbsp;:update_history&nbsp;&nbsp;def&nbsp;update_history &nbsp;&nbsp;&nbsp;&nbsp;histories.create(:date=>Time.now,&nbsp;:username=>&nbsp;User.current.username)&nbsp; &nbsp;&nbsp;endend

慕妹3242003

如果用户创建了一个项目,该项目是否应该有一个belongs_to :user子句?这样你就after_update可以做到History.create&nbsp;:username&nbsp;=>&nbsp;self.user.username
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Ruby