Rails 3手动修改密码 [英] Rails 3 Devise manually change password

查看:109
本文介绍了Rails 3手动修改密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的rails应用程序中使用设计。但是我不明白如何给用户更改密码。我需要一个带有旧密码,新密码和新密码确认的表单。我该怎么做?



如果我在/个人资料页面上使用默认设计表单

 <%= 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 

但是对于#<#< Class:0x59b9200> ,然后(在评论devise_error_messages!行之后)有
undefined方法'devise_error_messages!'
未定义的方法'#'为#< 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 => 密码已更新!
else
render:edit
end
end
end

并使用此问题的建议:渲染Devise编辑密码表单



插入此代码

 <%=呈现:template => 'password / edit',
:locals => {
:resource => current_user,
:resource_name =>用户}%>

进入/个人资料页面。



password / edit.html.erb包含此代码

 < h2>更改密码< / h2> 
<%#raise resource.inspect%>
<%= form_for(resource,:as => resource_name,:url => password_path(resource_name),:html => {:method =>:put})do | f | %GT;
<%#devise_error_messages! %GT;
<%= f.hidden_​​field:reset_password_token%>

< p><%= f.label:password,新密码%>< br />
<%= password_field_tag:name => 用户[口令] %>< / p为H.
<%= password_field_tag:name => 用户[password_confirmation] %>< / p为H.

< p><%= f.submit更改我的密码%>< / p>
<%end%>

<%= render:partial => devise / shared / links%>

但是,渲染表单对于action属性具有/ profile值,并且提交此表单什么也不做。 p>

解决方案

您的表单应为 password / edit.html.erb

 <%#raise resource.inspect%> 
<%= form_for(resource,:as => resource_name,:url => password_path(resource_name),:html => {:method =>:put})do | f | %GT;
<%#devise_error_messages! %GT;
<%= f.hidden_​​field:reset_password_token%>

< p><%= f.label:current_password%>< br />
<%= f.password_field:current_password%>< / p>

< p><%= f.label:password,新密码%>< br />
<%= f.password_field:password%>< / p>

< p><%= f.label:password_confirmation,确认新密码%>< br />
<%= f.password_field:password_confirmation%>< / p>

< p><%= f.submit更改我的密码%>< / p>
<%end%>

VIEW

 < h3>更改密码< / h3> 
< hr />
<%= render:template => 'password / 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
如果@ user.update_with_password(params [:user])
sign_in(@user,:bypass => true)
redirect_to root_path,:notice => 您的密码已更新!
else
render:edit,:locals => {:resource => @user,:resource_name => user}
end
end
end

路线

  devise_for:users,:controllers => {:passwords => 密码} do 
end

资源:密码

它适用于我;


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 was undefined 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.

解决方案

Your form should be i.e. 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 %>

VIEW:

<h3>Change password </h3>
<hr/>
<%= render :template => 'passwords/edit', 
                    :locals => { :resource => current_user, :resource_name => "user" } %>

Controller:

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

Routes:

devise_for :users ,:controllers => {:passwords => "passwords"} do
  end

  resources :passwords

It works for me;

这篇关于Rails 3手动修改密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆