2012-06-20 79 views
2

正如標題所述,我正在使用設計。我遇到了這個描述這樣做的過程的這個 link。 我有這個用戶控制器:設計自定義密碼更改

def update_password 
     @user = User.find(current_user.id) 
     #raise @user.to_yaml 
     if @user.update_attributes(params[:user]) 
     # Sign in the user by passing validation in case his password changed 
     sign_in @user, :bypass => true 
     #flash[:notice] = "Password Successfully Changed" 
     redirect_to settings_path 
     else 
     flash[:notice] = @user.errors.full_messages 
     render password_path 
     end 
    end 

忽略#raise @user.to_yaml。 而這樣的觀點:

 <h1>Change Password</h1> 

     <%= form_for(:user, :url => { :action => 'update_password'}) do |f| %> 
      <div> 
       <%= f.label :current_password %> 
       <%= f.password_field :current_password %> 
      </div> 

      <div> 
       <%= f.label :password %> 
       <%= password_field_tag :password %> 
      </div> 

      <div> 
       <%= f.label :password_confirmation %> 
       <%= f.password_field :password_confirmation %> 
      </div> 

      <div> 
       <%= f.submit "Change Password" %> 
      </div> 
     <% end %> 

我有映射到該控制器的方法這一觀點:

def password 
     @user = current_user 
    end 

爲了分離動作和圖,我的配置/ routes.rb中是這樣的: match '/password' => 'users#password', :as => 'password'

當我單擊窗體中的「更改密碼」按鈕時出現問題。 這是它給我帶來的鏈接:「http:// localhost:3000/assets?action = update_password & controller = users」,我得到錯誤「找不到id = assets的用戶」

I不知道爲什麼它以這種方式行事,以及我做錯了什麼,因爲我已經複製了網頁所說的內容。有沒有人有這方面的經驗,可以幫助我?謝謝!

回答

1

如果您使用Rails 3或更新你可以設置使用資源的語法您的路線:

resources :users do 
    member do # These routes will apply to specific model instances 
    get 'password' # /users/:id/password 
    put 'update_password' # /users/:id/updated_password 
    end 
end 

那麼你就可以在你的form_for聲明中使用的路徑幫手

<%= form_for @user, :url => update_password_user_path(@user) do |form| %> 
... 
<% end %> 
+0

謝謝對於幫助,但現在當我點擊提交按鈕時,我被':Missing template/password with'{:locale => [:en],:formats => [:html],:handlers => [:erb, :builder,:arb,:coffee]}。'。出於某種原因,它需要一個我不想要的視圖。它似乎不是在update_with_password之後重定向。 – Vasseurth

+0

其實我能解決這個問題。現在的問題在於每一次,無論它是什麼輸入。我收到錯誤「密碼不能爲空,密碼確認不匹配」,即使它們匹配 – Vasseurth

+1

這個「<%= password_field_tag:password%>'應該是'<%= f.password_field:password%>'否則它不會被限制在用戶的params中。如果你在做改變之前檢查html,我想你會明白我的意思。 – patrickmcgraw