【发布时间】:2011-07-07 08:32:33
【问题描述】:
我尝试在我的 rails 应用程序中使用设计。但我不明白如何为用户提供更改密码的功能。我需要一个包含“旧密码”、“新密码”和“新密码确认”字段的表格。我该怎么做?
如果我在“/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
但是有
undefined method 'devise_error_messages!' for #<#<Class:0x59b9200> 然后(在评论 devise_error_messages! 行之后)
undefined method 'password' for #<Class:0x59b9200> 错误。
我尝试使用自己的 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
并使用这个问题的建议:Rendering the Devise edit Password Form
插入此代码
<%= 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" %>
但是呈现的表单对于 action 属性有“/profile”值并且提交这个表单什么都不做。
【问题讨论】:
-
如果正确,您应该接受其中一个答案。
标签: ruby-on-rails-3 passwords devise edit