Rails的验证,记住我 [英] Rails Auth, Remember me
问题描述
我试图通过添加以下瑞恩记得我的功能,我的登录表单贝茨的教程。然而,当我尝试访问我的网页我得到这个错误:
找不到用户用
的auth_token =
块引用>我认为这个问题是从我application_controller.rb未来
类的ApplicationController< ActionController的基地::
protect_from_forgery
是helper_method:CURRENT_USER 私人的 高清CURRENT_USER
@current_user || = User.find_by_auth_token(饼干[:AUTH_TOKEN])!如果Cookie [:AUTH_TOKEN]
结束
结束这里的code从我user.rb
类用户< ActiveRecord的::基地
attr_accessible:用户名,:电子邮件,:密码:password_confirmation
has_secure_password validates_ presence_of:用户名
validates_uniqueness_of:用户名
validates_ presence_of:电子邮件
validates_uniqueness_of:电子邮件
validates_confirmation_of:密码
validates_ presence_of:密码:上= GT; :创建 before_create {generate_token(:AUTH_TOKEN)} 高清send_password_reset
generate_token(:password_reset_token)
self.password_reset_sent_at = Time.zone.now
保存!
UserMailer.password_reset(个体经营).deliver
结束 高清generate_token(列)
开始
自[专栏] = SecureRandom.urlsafe_base64
结束而User.exists(列=>自[专栏])?
结束
结束下面是我的user_controller.rb
类UsersController< ApplicationController中
高清新
@user = User.new
结束 高清节目
@user = User.find(PARAMS [:ID])
结束 打造高清
@user = User.new(PARAMS [:用户])
如果@ user.save
redirect_to的@user,:通知=> 注册!
其他
渲染新
结束
结束
结束最后,这里是我sessions_controller
类SessionsController< ApplicationController中
高清新
结束 打造高清
用户= User.find_by_username(PARAMS [:用户名])
如果用户放大器;&安培; user.authenticate(PARAMS [:密码])
如果PARAMS [:remember_me]
cookies.permanent [:AUTH_TOKEN] = user.auth_token
其他
饼干[:AUTH_TOKEN] = user.auth_token
结束
redirect_to的root_url,:通知=> 登录!
其他
flash.now.alert =无效的电子邮件或密码
渲染新
结束
结束 DEF破坏
cookies.delete(:AUTH_TOKEN)
redirect_to的root_url,:通知=> 登出!
结束
结束我唯一改变的是找到通过用户名,而不是电子邮件的用户,但我不明白为什么这会破坏它。任何人有什么想法?
修改
我的数据库由以下迁移填充
类调用createUsers< ActiveRecord的::迁移
高清变化
CREATE_TABLE:用户做| T |
t.string:用户名
t.string:电子邮件
t.string:password_digest t.timestamps
结束
结束
结束类AddAuthTokenToUsers< ActiveRecord的::迁移
高清变化
add_column:用户:AUTH_TOKEN,:字符串
结束
结束类AddPasswordResetToUsers< ActiveRecord的::迁移
高清变化
add_column:用户:password_reset_token,:字符串
add_column:用户:password_reset_sent_at,时间:DATETIME
结束
结束
结束下面是根据终端窗口中的错误:
::的ActionView ::模板错误(找不到用户提供的auth_token =):
1:< H1>家庭和LT; / H1>
2:
3:或其可格ID =user_nav>
4:LT;%如果CURRENT_USER%GT;
5:在登录为<%= current_user.email%GT;
6:<%=的link_to退出,logout_path%GT;
7:或其可%其他%GT;
应用程序/控制器/ application_controller.rb:8:`CURRENT_USER
应用程序/视图/页/ home.html.erb:4:`_app_views_pages_home_html_erb___725535246_2189699680编辑2
下面是code为home.html.erb文件
< H1>家庭和LT; / H1>< DIV ID =user_nav>
<%如果CURRENT_USER%GT;
(%)= current_user.email%>在为&lt记录;
<%=的link_to退出,logout_path%GT;
<%其他%GT;
<%=的link_to注册,signup_path%GT;要么
<%=的link_to登录,login_path%GT;
<%结束%GT;
< / DIV>
解决方案这可能不是-THE-的答案,但你尝试过这样的:
< H1>家庭和LT; / H1>< DIV ID =user_nav>
<%如果current_user.auth_token.nil!? %GT;
(%)= current_user.email%>在为&lt记录;
<%=的link_to退出,logout_path%GT;
<%其他%GT;
<%=的link_to注册,signup_path%GT;要么
<%=的link_to登录,login_path%GT;
<%结束%GT;
< / DIV>我怀疑,出于某种原因,
的auth_token
为空为CURRENT_USER。您也可以尝试修改逻辑是这样的:< DIV ID =user_nav>
铜零:?&下;%= current_user.nil%GT;
cu.at.nil:&下;%= current_user.auth_token.nil?如果CURRENT_USER零%GT!?;
<%=的link_to退出,logout_path%GT;
<%=的link_to注册,signup_path%GT;要么
<%=的link_to登录,login_path%GT;
< / DIV>...当你在调试这个问题。至少,应该让你多一点信息和页面加载,而你进一步挖掘。
I'm trying to add remember me functionality to my login form by following Ryan Bates's tutorial. However when I try to access my pages I get this error:
Couldn't find User with
auth_token =
I think the problem is coming from my application_controller.rb
class ApplicationController < ActionController::Base protect_from_forgery helper_method :current_user private def current_user @current_user ||= User.find_by_auth_token!(cookies[:auth_token]) if cookies[:auth_token] end end
Here's the code from my user.rb
class User < ActiveRecord::Base attr_accessible :username, :email, :password, :password_confirmation has_secure_password validates_presence_of :username validates_uniqueness_of :username validates_presence_of :email validates_uniqueness_of :email validates_confirmation_of :password validates_presence_of :password, :on => :create before_create { generate_token(:auth_token) } def send_password_reset generate_token(:password_reset_token) self.password_reset_sent_at = Time.zone.now save! UserMailer.password_reset(self).deliver end def generate_token(column) begin self[column] = SecureRandom.urlsafe_base64 end while User.exists?(column => self[column]) end end
Here's my user_controller.rb
class UsersController < ApplicationController def new @user = User.new end def show @user = User.find(params[:id]) end def create @user = User.new(params[:user]) if @user.save redirect_to @user, :notice => "Signed up!" else render "new" end end end
And finally here is my sessions_controller
class SessionsController < ApplicationController def new end def create user = User.find_by_username(params[:username]) if user && user.authenticate(params[:password]) if params[:remember_me] cookies.permanent[:auth_token] = user.auth_token else cookies[:auth_token] = user.auth_token end redirect_to root_url, :notice => "Logged in!" else flash.now.alert = "Invalid email or password" render "new" end end def destroy cookies.delete(:auth_token) redirect_to root_url, :notice => "Logged out!" end end
The only thing I changed was finding the user by username instead of email but I can't see why that would break it. Anyone got any ideas?
Edit
My database is populated by the following migrations
class CreateUsers < ActiveRecord::Migration def change create_table :users do |t| t.string :username t.string :email t.string :password_digest t.timestamps end end end class AddAuthTokenToUsers < ActiveRecord::Migration def change add_column :users, :auth_token, :string end end class AddPasswordResetToUsers < ActiveRecord::Migration def change add_column :users, :password_reset_token, :string add_column :users, :password_reset_sent_at, :datetime end end end
Here is the error according to the terminal window:
ActionView::Template::Error (Couldn't find User with auth_token = ): 1: <h1>Home</h1> 2: 3: <div id="user_nav"> 4: <% if current_user %> 5: Logged in as <%= current_user.email %> 6: <%= link_to "Log out", logout_path %> 7: <% else %> app/controllers/application_controller.rb:8:in `current_user' app/views/pages/home.html.erb:4:in `_app_views_pages_home_html_erb___725535246_2189699680'
Edit 2
Here is the code for the home.html.erb file
<h1>Home</h1> <div id="user_nav"> <% if current_user %> Logged in as <%= current_user.email %> <%= link_to "Log out", logout_path %> <% else %> <%= link_to "Sign up", signup_path %> or <%= link_to "Log in", login_path %> <% end %> </div>
解决方案This may not be -the- answer, but have you tried something like:
<h1>Home</h1> <div id="user_nav"> <% if !current_user.auth_token.nil? %> Logged in as <%= current_user.email %> <%= link_to "Log out", logout_path %> <% else %> <%= link_to "Sign up", signup_path %> or <%= link_to "Log in", login_path %> <% end %> </div>
I suspect that, for some reason,
auth_token
is null for current_user. You can also try modifying the logic to something like:<div id="user_nav"> cu nil?:<%=current_user.nil?%> cu.at.nil?:<%=current_user.auth_token.nil? if !current_user.?nil%> <%= link_to "Log out", logout_path %> <%= link_to "Sign up", signup_path %> or <%= link_to "Log in", login_path %> </div>
... while you are debugging this issue. At least that should get you a little more information and a page that loads while you dig further.
这篇关于Rails的验证,记住我的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!