我尝试在我的rails app中使用设计。但我不明白我如何授予用户更改密码的功能。我需要一个带有“旧密码”,“新密码”和“新密码确认”字段的表单。我该怎么做?
如果我在“/ 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”值,并且提交此表单不执行任何操作。
答案 0 :(得分:11)
您的表单应为 password / edit.html.erb
<%# 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 :current_password %><br />
<%= f.password_field :current_password %></p>
<p><%= f.label :password, "New password" %><br />
<%= f.password_field :password %></p>
<p><%= f.label :password_confirmation, "Confirm new password" %><br />
<%= f.password_field :password_confirmation %></p>
<p><%= f.submit "Change my password" %></p>
<% end %>
查看:
<h3>Change password </h3>
<hr/>
<%= render :template => 'passwords/edit',
:locals => { :resource => current_user, :resource_name => "user" } %>
<强>控制器强>:
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 => "Your Password has been updated!"
else
render :edit,:locals => { :resource => @user, :resource_name => "user" }
end
end
end
<强>路线强>:
devise_for :users ,:controllers => {:passwords => "passwords"} do
end
resources :passwords
它对我有用;
答案 1 :(得分:0)
如果您在User周围添加引号会发生什么情况:
<%= render :template => 'passwords/edit',
:locals => {
:resource => current_user,
:resource_name => "User" } %>