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