2011-07-07 113 views
5

我嘗試在我的Rails應用程序中使用usung。但我不明白我怎麼能給用戶功能來更改他的密碼。我需要一個帶有「舊密碼」,「新密碼」和「新密碼確認」字段的表單。我該怎麼做?Rails 3設計手動更改密碼

如果我的 「/個人資料」 頁面使用默認色器件形式

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

成 「/資料」 頁面。

密碼/ 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屬性和submiting這種形式的價值無能爲力。

+1

如果您的權利正確,您應該接受其中一個答案。 –

回答

0

如果你加引號的用戶,因爲這樣會發生什麼:

<%= render :template => 'passwords/edit', 
        :locals => { 
         :resource => current_user, 
         :resource_name => "User" } %> 
+0

現在我有一個「未定義的方法User_password_path」錯誤。 – Ardentum

+0

我認爲問題在於從Devise控制器訪問來自其他控制器的「資源」。試着讓你的PasswordsController繼承Devise PasswordsController。 – joseph

11

你的表格應即密碼/ 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" } %> 

控制器

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 

這對我的作品;