ruby-on-rails – 使用Devise重置密码问题

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – 使用Devise重置密码问题前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在我的Rails应用程序中使用Devise并且遇到重置密码的问题.

当我尝试重置密码时,我发送了一封电子邮件,其中包含重置密码的链接.在使用新密码填写表单后,我在Chrome中收到错误“网页有重定向循环”,我的日志中出现以下错误

Started GET "/users/password/edit?reset_password_token=[FILTERED]" for 127.0.0.1 at 2013-12-19 14:22:05 -0500
Processing by Devise::PasswordsController#edit as HTML
  Parameters: {"reset_password_token"=>"[FILTERED]"}
  User Load (0.4ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
Redirected to http://localhost:3000/users/password/edit?reset_password_token=JatMT1fE-fQwsCWsEdy6
Filter chain halted as :require_no_authentication rendered or redirected
Completed 302 Found in 1.8ms (ActiveRecord: 0.4ms)

我似乎无法找到有关如何解决此问题的任何信息.

user.rb

class User < ActiveRecord::Base
 ...
 devise :database_authenticatable,:registerable,:recoverable,:rememberable,:trackable,:validatable,:token_authenticatable,:confirmable,:lockable
 ...
end

devise.rb

Devise.setup do |config|
  ...
  config.reset_password_within = 6.hours
  ...
end

的routes.rb

Build::Application.routes.draw do
  devise_for :users,:controllers => {:registrations => :registrations}

  devise_scope :user do
      post 'registrations' => 'registrations#create',:as => 'register'
      post 'sessions' => 'sessions#create',:as => 'login'
      delete 'sessions' => 'sessions#destroy',:as => 'logout'
    end

 resources :users do
     match 'users/:id' => 'users#username'
     get 'validate_username',on: :collection
     get 'validate_email',on: :collection
     get 'edit_profile',on: :member
     get :projects,on: :member
     get :favorites,on: :member
     get :collections,on: :member
     member do
      get :follow
      get :unfollow
      get :following
      get :followers
     end
  end
end

registrations_controller.rb

class RegistrationsController < Devise::RegistrationsController

    skip_before_filter :verify_authenticity_token,:if => Proc.new { |c| c.request.format == 'application/json' }

    respond_to :json

    def create
        user = User.new(params[:user])
        Rails.logger.info(user.inspect)
        # comment out following line to re-enable confirmation
        # resource.skip_confirmation!

        if user.save
          sign_in user
          render :status => 200,:json => { :success => true,:info => "Registered",:data => { :user => user,:auth_token => current_user.authentication_token } }
        else
            redirect_to new_user_registration_path,notice: user.errors.full_messages[0]
            Rails.logger.info(user.errors.inspect)
          # render :status => :unprocessable_entity,#        :json => { :success => false,#                   :info => resource.errors,#                   :data => {} }
        end
    end

    def update
        @user = User.find(current_user.id)
        successfully_updated = if needs_password?(@user,params)
            @user.update_with_password(params[:user])
        else
            # remove the virtual current_password attribute 
            params[:user].delete(:current_password)
            @user.update_without_password(params[:user])
        end

        if successfully_updated
            if params[:update_email]
                set_flash_message :alert,:signed_up_but_unconfirmed
                redirect_to after_update_path_for(@user)
            else            
                set_flash_message :notice,:updated
                sign_in @user,:bypass => true
                redirect_to after_update_path_for(@user)
            end
        else
            redirect_to :back,alert: resource.errors.full_messages[0]
        end
    end

        private

    # check if we need password to update user data
    def needs_password?(user,params)
        !params[:profile]
    end

    protected

    def after_update_path_for(resource)
        user_path(resource)
    end

end

解决方法

如果是密码编辑请求,请检查ApplicationController中的after_sign_in_path_for并将重定向添加到root_url(不是:back或request.env [‘HTTP_REFERER’]).

猜你在找的Ruby相关文章