我有标准的设计方法来更改我的代码中的用户密码:
<%= link_to "Change your password", edit_user_password_path, :class => "" %>
然后我点击我重定向到主页面的链接,并显示用户已登录的消息,控制台中的输出为:
Started GET "/users/password/edit" for 127.0.0.1 at 2015-06-11 16:11:04 +0300
Processing by Devise::PasswordsController#edit as HTML
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 4 LIMIT 1
Role Load (0.2ms) SELECT "roles".* FROM "roles" INNER JOIN "roles_users" ON "roles"."id" = "roles_users"."role_id" WHERE "roles_users"."user_id" = 4 AND "roles"."name" = 'admin' LIMIT 1
Redirected to http://localhost:3000/admin
Filter chain halted as :require_no_authentication rendered or redirected
Completed 302 Found in 4ms (ActiveRecord: 0.3ms)
在我的路线中,我有:
devise_for :users, :controllers => { :registrations => "registrations", sessions: "sessions" }
resources :users, :only => [:show]
registartions控制器:
class RegistrationsController < Devise::RegistrationsController
def new
build_resource({:unconfirmed_company_id => params[:unconfirmed_company_id], :email => params[:email], :company_id => params[:company_id]})
respond_with self.resource
end
def build_resource(hash=nil)
super
if hash
self.resource.company_id = hash[:company_id]
self.resource.unconfirmed_company_id = hash[:unconfirmed_company_id]
self.resource.email = hash[:email]
end
self.resource
end
end
答案 0 :(得分:2)
在routes.rb
中 devise_for :users, :controllers => { :registrations => "registrations", :sessions => "sessions", :passwords => "passwords", :confirmations => "confirmations"}
如果您使用最新的具有强参数的Devise,则应将此行添加到ApplicationController.rb
class ApplicationController < ActionController::Base
before_filter :configure_permitted_parameters, if: :devise_controller?
....
def configure_permitted_parameters
devise_parameter_sanitizer.for(:account_update) { |u|
u.permit(:password, :password_confirmation, :current_password)
}
end
end
答案 1 :(得分:2)
因为devise的edit_user_password功能用于密码恢复。
要更改已登录用户的密码,请使用dependencies
模块。