在rails上的ruby中使用备用更新方法路由错误

时间:2012-01-13 12:23:35

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-3.1 rubygems

Ruby on rails 3.1.3

我正在尝试找出我的users_controller的change_password方法的正确路由。

我目前收到此错误:

Routing Error

No route matches {:action=>"change_password", :controller=>"users", :format=>#<User id: 1, email: "foo@bar.baz", encrypted_password: "$2a$10$lgmrRTYFUUrWStLl1Y.Oo.LqQ2Ybxa29YkDFw61/KG9O...", password_salt: "$2a$10$lgmrRTYFUUrWStLl1Y.Oo.", username: "foobar", created_at: "2012-01-07 23:02:29", updated_at: "2012-01-13 11:16:45", password_reset_token: nil, password_reset_sent_at: "2012-01-08 12:23:30">}

在users_controller中:

def change_password
      @user = current_user
      @user_password_matches = User.authenticate(@user.email, params[:current_password])
      if @user_password_matches.update_attributes(params[:user])
        login @user
        format.js   { render :js => "window.location = '#{settings_account_path}'" } 
        flash[:success] = "Password updated" 
      else
        format.js   { render :form_errors }

      end
    end

内幕:

<%= form_for @user, :remote => true, :url => change_password_path(@user) do |f| %>
Current password: <%= password_field_tag :current_password, :placeholder => "Password"  %><br />
New password: <%= f.password_field :password, :placeholder => "Password"  %><br />
Confirm password: <%= f.password_field :password_confirmation, :placeholder => "Confirm Password"  %><br />
<%= f.submit 'update' %>

路线:

resources :users do
  member do
    put :change_password
  end
end
  resources :users
  resources :sessions
  resources :passwords
  resources :profiles


  root :to                   => "users#new"
  match 'success'            => "users#success"
  match 'login'              => "sessions#new"
  match 'logout'             => "sessions#destroy"
  match 'reset_password'     => "passwords#new"
  match 'setup_new_password' => "passwords#edit"
  match 'settings', :to      => "users#settings"


  match "/settings/account", :to => "users#account"
  match "/settings/edit_profile", :to => "profiles#edit_profile"


  match '/:username', :controller => 'users', :action => 'show'

我打算做的只是更新db中的属性,如果用户输入密码(:current_password)成功通过身份验证(我使用相同的方法登录用户,这将确认密码与存储在数据库中的密码相匹配)如果它确实需要用户的参数形式,在这种情况下是新散列的密码并将其存储在数据库中..

亲切的问候

1 个答案:

答案 0 :(得分:2)

您的表单中的路径名称错误,它是change_password_user_path,而不是change_password_path

使用rake routes查看路线的名称。此外,您不需要自定义路线,请查看我之前问题的答案。