2012-08-06 32 views
1

在我的ruby on rails項目中,我使用devise作爲身份驗證插件。現在,我已經添加了更改密碼功能項目,並提交表單的時候,我得到一個錯誤No route matches "https://stackoverflow.com/users/update_password"在Ruby on Rails中沒有路由匹配

這是我更新密碼的代碼

def update_password 
    @user = User.find(current_user.id) 
    if @user.update_attributes(params[:user]) 
     # Sign in the user by passing validation in case his password changed 
     sign_in @user, :bypass => true 
     redirect_to root_path 
    else 
     render "edit" 
    end 
    end 

這是我的路線詳情

resources :users, :only => [] do 
     collection do 
      get 'current' 
      get 'edit' 
      post 'update_password' 
     end 
    end 

,這是我的編輯形式

<div id="loginForm"> 
    <div id="loginHeader"><h3>Change Password</h3></div> 
    <div id="loginContent"> 
     <%= form_for(@user, :url => { :action => "update_password" }) do |f| %> 
      <p><%= f.password_field :current_password, :class => "login_txt", :placeholder => "We need your current password to confirm your changes", :autocomplete => "off" %></p> 
      <p><%= f.password_field :password, :class => "login_txt", :placeholder => "New password", :autocomplete => "off" %></p> 
      <p><%= f.password_field :password_confirmation, :class => "login_txt", :placeholder => "Confirm password", :autocomplete => "off" %></p> 
      <p> 
       <input id="user_submit" name="commit" type="submit" value="Update Password" class="login_btn" /> 
      </p> 
     <% end %> 
    </div> 
</div> 

人幫助我如何解決這個問題

非常感謝

+1

可以運行'耙路線「並粘貼與Devise相關的輸出? – 2012-08-06 05:19:38

回答

2

在你routes.rb定義update_password作爲集合(/users/update_password)一個POST操作,但在你的表格,你是隱其定義爲一個PUT操作時您由於@user已經存在,因此將@user傳遞給form_for

我沒有測試過這一點,但我相信,改變你的收集路線從POST到PUT應該解決這個問題:

resources :users, :only => [] do 
    collection do 
    get 'current' 
    get 'edit' 
    put 'update_password' 
    end 
end 

參見:Rails 3.0.10, customer routes, post and form_for tag

+0

非常感謝,現在提到的錯誤是固定的,但密碼不更新 – Mujahid 2012-08-06 05:35:09

+0

我不使用自己設計,所以我不知道它是如何工作的,但也許問題是'password'屬性受到保護?如果是這種情況,您需要在用戶模型中添加'attr_accessible:password,:password_confirmation',或者您可以顯式地在控制器代碼中設置屬性。 – 2012-08-06 05:45:13

+1

也嘗試將'update_attributes'更改爲'update_attributes!'並查看它返回的錯誤。 – 2012-08-06 05:46:49

相關問題