【问题标题】:Rails Auth, Remember meRails Auth,记住我
【发布时间】:2012-03-22 08:51:46
【问题描述】:

我正在尝试通过关注Ryan Bates's tutorial 在我的登录表单中添加记住我的功能。但是,当我尝试访问我的页面时,我收到此错误:

找不到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

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

【问题讨论】:

  • 您可以将错误消息添加到您的帖子中吗?
  • 请用sql描述表,例如: sql> 描述用户;
  • 顺便说一句,您可以通过rails db 进入正在使用的任何数据库的 sql 提示符 - 非常棒!
  • 您的编辑很有帮助。 app/views/pages/home.html.erb 中可能会出现错误。你能发布那个代码吗?
  • 感谢 Perry 和 Michael,添加了 home.html.erb 文件内容

标签: ruby-on-rails authentication remember-me


【解决方案1】:

这可能不是答案,但您是否尝试过类似的方法:

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

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

<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 是一个未定义的参数。
  • 您是否确认此时已定义 current_user?
  • 如果您看到“auth_token 是一个未定义的参数”,则删除包含“cu.at.nil?:" ... 看看 cu nil?: 为您显示什么。
  • 我想我假设您在运行“rake db:migrate”时发布的迁移工作正常,不是吗?您可以通过检查 db/schema.rb 并在 users 表中查找 auth_token 条目来确认。
  • 我决定去掉主文件中的 erb 只是为了让页面加载然后我意识到 SecureRandom.urlsafe_base64 提出了一个未定义的方法,所以我改变了它 SecureRandom.hex 似乎现在开始工作。我将尝试将您建议的代码添加回主页。感谢您提供有关调试的提示
【解决方案2】:

我有同样的问题。在将 auth_token 字段添加到用户数据库表之前,该问题仅发生在我的数据库中的用户身上。 App Couldn't find User with auth_token 作为老用户注册时没有为他们创建一个应用程序,因为当时没有实现此功能。

为了克服这个问题,我只是从我的数据库中删除了旧用户并重新注册。

但是不再自动登录的用户。为了克服这个问题,我改变了这一行:

session[:user_id] = @user.id

cookies[:auth_token] = @user.auth_token

因为我现在使用 cookie 而不是会话来登录用户。

【讨论】:

    【解决方案3】:

    我遇到了同样的问题,在阅读了 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
    

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

    不确定这是否是正确的方法,但我想我会把它扔在那里。

    谢谢 帕特里克

    【讨论】:

      猜你喜欢
      • 2012-09-08
      • 1970-01-01
      • 2015-06-12
      • 2011-09-20
      • 2015-08-08
      • 2016-06-23
      • 2019-05-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多