Rails 3 Devise 手动更改密码
我尝试在我的 Rails 应用程序中使用 devise 。但我不明白如何为用户提供更改密码的功能。我需要一个包含“旧密码”、“新密码”和“新密码确认”字段的表单。我该怎么做?
如果我在“/profile”页面上使用默认设计形式
<%= render :template => 'devise/passwords/edit',
:locals => {
:resource => my_user_model_variable,
:resource_name => my_user_model_name } %>
在 user.rb 包含行
attr_accessible :email, :password, :password_confirmation, :remember_me
但是有 未定义的方法“devise_error_messages!”对于 #<#
然后(在注释 devise_error_messages! 行之后) #
错误未定义方法“密码”。
我尝试使用我自己的PasswordsController:
class PasswordsController < ApplicationController
before_filter :authenticate_user!
def edit
@user = current_user
end
def update
@user = current_user
raise params.inspect
if @user.update_with_password(params[:user])
sign_in(@user, :bypass => true)
redirect_to root_path, :notice => "Password updated!"
else
render :edit
end
end
end
并使用这个问题的建议:渲染设计编辑密码表单
将此代码插入
<%= render :template => 'passwords/edit',
:locals => {
:resource => current_user,
:resource_name => User } %>
“/profile”页面。
passwords/edit.html.erb 包含此代码
<h2>Change your password</h2>
<%# raise resource.inspect %>
<%= form_for(resource, :as => resource_name, :url => password_path(resource_name), :html => { :method => :put }) do |f| %>
<%# devise_error_messages! %>
<%= f.hidden_field :reset_password_token %>
<p><%= f.label :password, "New password" %><br />
<%= password_field_tag :name => "user[password]"%></p>
<%= password_field_tag :name => "user[password_confirmation]"%></p>
<p><%= f.submit "Change my password" %></p>
<% end %>
<%= render :partial => "devise/shared/links" %>
,但呈现的表单的操作属性具有“/profile”值,并且提交此表单不执行任何操作。
I try to usung devise in my rails app. But i don't understand how can i give user functionality to change his password. I need a form with fields "old password", "new password" and "new password confirmation". How can i do it?
If i use default devise form on "/profile" page
<%= render :template => 'devise/passwords/edit',
:locals => {
:resource => my_user_model_variable,
:resource_name => my_user_model_name } %>
In user.rb contain line
attr_accessible :email, :password, :password_confirmation, :remember_me
But there wasundefined method 'devise_error_messages!' for #<#<Class:0x59b9200>
and then (after commenting devise_error_messages! line)undefined method 'password' for #<Class:0x59b9200>
errors.
I try to use my own PasswordsController:
class PasswordsController < ApplicationController
before_filter :authenticate_user!
def edit
@user = current_user
end
def update
@user = current_user
raise params.inspect
if @user.update_with_password(params[:user])
sign_in(@user, :bypass => true)
redirect_to root_path, :notice => "Password updated!"
else
render :edit
end
end
end
and use advise from this question: Rendering the Devise edit Password Form
insert this code
<%= render :template => 'passwords/edit',
:locals => {
:resource => current_user,
:resource_name => User } %>
into "/profile" page.
passwords/edit.html.erb contain this code
<h2>Change your password</h2>
<%# raise resource.inspect %>
<%= form_for(resource, :as => resource_name, :url => password_path(resource_name), :html => { :method => :put }) do |f| %>
<%# devise_error_messages! %>
<%= f.hidden_field :reset_password_token %>
<p><%= f.label :password, "New password" %><br />
<%= password_field_tag :name => "user[password]"%></p>
<%= password_field_tag :name => "user[password_confirmation]"%></p>
<p><%= f.submit "Change my password" %></p>
<% end %>
<%= render :partial => "devise/shared/links" %>
But rendered form has "/profile" value for action attribute and submiting this form do nothing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的表单应该是 password/edit.html.erb
VIEW:
Controller:
Routes:
它对我有用;
Your form should be i.e. password/edit.html.erb
VIEW:
Controller:
Routes:
It works for me;
如果您在 User 周围添加引号,会发生什么情况,如下所示:
What happens if you add quotes around User, as such: