- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有3张 table
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=> ?)
end
end
我在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
end
end
我没有使用任何身份验证插件。如果可能的话,如果有人能告诉我如何在不使用插件(如 userstamp 等)的情况下实现这一目标,我将不胜感激。
最佳答案
声明模块
module Current
thread_mattr_accessor :user
end
分配当前用户
class ApplicationController < ActionController::Base
around_action :set_current_user
def set_current_user
Current.user = current_user
yield
ensure
# to address the thread variable leak issues in Puma/Thin webserver
Current.user = nil
end
end
现在您可以将当前用户称为 Current.user
关于 thread_mattr_accessor 的文档
在模型中访问 current_user
并不常见。话虽这么说,这是一个解决方案:
class User < ActiveRecord::Base
def self.current
Thread.current[:current_user]
end
def self.current=(usr)
Thread.current[:current_user] = usr
end
end
在 ApplicationController
的 around_filter
中设置 current_user
属性。
class ApplicationController < ActionController::Base
around_filter :set_current_user
def set_current_user
User.current = User.find_by_id(session[:user_id])
yield
ensure
# to address the thread variable leak issues in Puma/Thin webserver
User.current = nil
end
end
身份验证成功后设置current_user
:
def login
if User.current=User.authenticate(params[:username], params[:password])
session[:user_id] = User.current.id
flash[:message] = "Successfully logged in "
redirect_to( :action=>'home')
else
flash[:notice] = "Incorrect user/password combination"
redirect_to(:action=>"login")
end
end
最后引用Item
的update_history
中的current_user
。
class Item < ActiveRecord::Base
has_many :histories
after_create :update_history
def update_history
histories.create(:date=>Time.now, :username=> User.current.username)
end
end
关于ruby-on-rails - 访问模型中的current_user,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2513383/
Devise自述文件没有提及任何关于 @current_user 和 I don't see any reference to it in Devise's source code 的内容- 也不在我
我关注this post 。在我看来: - if current_user - if current_user.present? - if current_user.exists? - if curr
我关注this post 。在我看来: - if current_user - if current_user.present? - if current_user.exists? - if curr
刚开始使用我的应用程序身份验证设计开发,但是在从模型访问 current_user 时遇到了麻烦。我从 Controller 访问它没有问题。 对于我的场景,我需要获取当前用户 ID 以跟踪谁编辑了博
我在我的flask应用程序中使用flask_login扩展名登录用户。如您所知,此扩展名具有存储current_user的变量。该代码可以正常运行,除非要进行测试。 当我测试代码(使用unittest
我想用 Swift 为 OS X 应用程序定义一个变量。 但我总是收到此错误:“CURRENT_USER 的重新声明无效”。 这是我写的: var CURRENT_USER: Firebase? 我也
例如我有一个用户和一个地址,关系如下: class User < ActiveRecord::Base devise :database_authenticatable, :registerabl
我正在按照 teamtreehouse.com guide 构建我的第一个 Ruby on Rails 应用程序(ruby 2.0.0p353) . 我正在使用设计 3.2.2 devise在 app
我对 How to customize Devise to store more informations in the session? 有同样的问题 当我使用 current_user和 user
要为我的问题提供更多背景信息,请参阅此 Github 问题 - https://github.com/getsentry/raven-ruby/issues/144 我正在使用 raven这是一个错误
鉴于以下模型: Room (id, title) RoomMembers (id, room_id) RoomFeed, also an observer 当房间标题更新时,我想创建一个 RoomFe
我在 Rails 应用程序的 lib 目录中创建了一个类。该类需要访问 Devise 的 current_user。 我还应该指出,这个 lib 类经常从模型代码中访问,因此不能将 current_u
在 Flask 中,我想了解如何创建一个行为类似于 current_user 的函数,因为它在任何地方( Controller 和 View )都可用,但我很难真正知道它是什么搜索以找到答案。 我能找
我目前正在阅读 RoR 教程(http://railstutorial.org/chapters/sign-in-sign-out#sec:signin_success 是相关部分),这似乎相当不错,
我在 rails5 中使用 Active Model Serializer 0.10.7 我想知道如何在序列化程序中访问设计 current_user。 current_user 应该默认设置为范围。
如何将参数传递给我的 current_user 重定向? # SessionHelper.rb def current_user if(user_id = session[:user_id]
默认列值存在问题... 以下作品: alter table chartdata.reddit_comments add modified_datetime timestamp default now(
我使用以下命令检查 PostgreSQL 数据库中用户的组或角色, select rolname from pg_authid where pg_has_role('some_username', r
我正在使用系统测试来验证以下流程是否按用户预期的方式工作: 在表格中注册 登录 访问帐户页面 更新账户信息 但是,我在创建用户后遇到错误: Puma caught this error: Couldn
我正在使用 Flask-Login,我想通过调用 current_user.is_authenticated() 检查用户是否已登录。 我的 Flask 应用程序被组织成蓝图,其中包含一个主要的 __
我是一名优秀的程序员,十分优秀!