繁体   English   中英

Rails Auth,记住我

[英]Rails Auth, Remember me

我试图通过遵循Ryan Bates的教程在我的登录表单中添加“记住我”功能。 但是,当我尝试访问我的页面时,出现此错误:

找不到具有auth_token =用户

我认为问题出在我的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

这是我的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

这是我的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

最后是我的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

我唯一更改的是通过用户名而不是电子邮件查找用户,但我看不出为什么会破坏它。 任何人有任何想法吗?

编辑

我的数据库由以下迁移填充

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

这是根据终端窗口的错误:

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'

编辑2

这是home.html.erb文件的代码

<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>

这可能不是答案,但是您是否尝试过以下方法:

<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>

我怀疑由于某种原因,对于current_user, auth_token为null。 您也可以尝试将逻辑修改为:

<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>

...在调试此问题时。 至少那应该为您提供更多的信息,并在您进一步挖掘时加载页面。

我遇到过同样的问题。 仅在将auth_token字段添加到用户数据库表之前,我的数据库中存在该用户的问题。 该应用程序Couldn't find User with auth_token因为年长的用户在注册时没有为他们创建一个用户,因为那时未实现此功能。

为了解决这个问题,我只是从数据库中删除了较老的用户并再次对其进行了注册。

但是,用户不再自动登录。 为了克服这一点,我更改了这一行:

session[:user_id] = @user.id

cookies[:auth_token] = @user.auth_token

就像我现在使用Cookie而不是使用会话登录用户一样。

我遇到了同样的问题,在阅读了所有这些文章和RailsCasts的想法之后,我尝试更新应用程序控制器以检查从cookie返回的auth_token是否确实包含某些东西并且它开始工作。

def current_user
    @current_user ||= User.find_by_auth_token!(cookies[:auth_token]) if cookies[:auth_token] && cookies[:auth_token].length > 0
end

我不确定cookie [:auth_token]是否返回nil并且条件语句没有捕获它,或者cookie [:auth_token]是否实际上返回空字符串,并且User.find_by_auth_token不喜欢它,即使它可以工作在控制台中。

不知道这是否是正确的方法,但我想我会把它扔出去。

谢谢帕特里克

暂无
暂无

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

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