2015-02-23 161 views
0

在我正在学习Hartl教程的过程中,对轨道很新颖。提出一个无法退出的问题似乎会弄清楚。Rails上的路由错误(没有路由匹配[get])

我设置了密码重置,但在URL点击生成的电子邮件,我得到以下错误时:

的ActionController :: RoutingError(无路由匹配[GET]“/ password_resets/$ 2A $ 10 $ oHhCh6ebtFXLYBKecGsTAu% 2FJDc.3FuUKDVacUQTsbLKpiPR6IWTkK /编辑“):

路由文件看起来像这样:

root 'static_pages#home' 
    get 'help' => 'static_pages#help' 
    get 'about' => 'static_pages#about' 
    get 'contact' => 'static_pages#contact' 
    get 'signup' => 'users#new' 
    get 'login' => 'sessions#new' 
    post 'login' => 'sessions#create' 
    delete 'logout' => 'sessions#destroy' 

    resources :users 
    resources(:account_activations, { :only => [:edit] }) 
    resources :password_resets,  only: [:new, :create, :edit, :update] 
end 

密码复位控制器

class PasswordResetsController < ApplicationController 
    before_action :get_user,   only: [:edit, :update] 
    before_action :valid_user,  only: [:edit, :update] 
    before_action :check_expiration, only: [:edit, :update] 

    def new 
    end 

    def create 
    @user = User.find_by(email: params[:password_reset][:email].downcase) 
    if @user 
     @user.create_reset_digest 
     @user.send_password_reset_email 
     flash[:info] = "Email sent with password reset instructions" 
     redirect_to root_url 
    else 
     flash.now[:danger] = "Email address not found" 
     render 'new' 
    end 
    end 

    def edit 
    end 

    def update 
    if password_blank? 
     flash.now[:danger] = "Password can't be blank" 
     render 'edit' 
    elsif @user.update_attributes(user_params) 
     log_in @user 
     flash[:success] = "Password has been reset." 
     redirect_to @user 
    else 
     render 'edit' 
    end 
    end 

    private 

    def user_params 
     params.require(:user).permit(:password, :password_confirmation) 
    end 

    # Returns true if password is blank. 
    def password_blank? 
     params[:user][:password].blank? 
    end 

    # Before filters 

    def get_user 
     @user = User.find_by(email: params[:email]) 
    end 

    # Confirms a valid user. 
    def valid_user 
     unless (@user && @user.activated? && 
       @user.authenticated?(:reset, params[:id])) 
     redirect_to root_url 
     end 
    end 

    # Checks expiration of reset token. 
    def check_expiration 
     if @user.password_reset_expired? 
     flash[:danger] = "Password reset has expired." 
     redirect_to new_password_reset_url 
     end 
    end 
end 

最后的邮件地址被设置像这样:

<%= link_to "Reset Password", edit_password_reset_url(@user.reset_token, email: @user.email) %> 

路线

    Prefix Verb URI Pattern        Controller#Action 
      sessions_new GET /sessions/new(.:format)     sessions#new 
       users_new GET /users/new(.:format)     users#new 
        root GET /          static_pages#home 
        help GET /help(.:format)       static_pages#help 
        about GET /about(.:format)      static_pages#about 
       contact GET /contact(.:format)      static_pages#contact 
       signup GET /signup(.:format)      users#new 
        login GET /login(.:format)      sessions#new 
         POST /login(.:format)      sessions#create 
       logout DELETE /logout(.:format)      sessions#destroy 
        users GET /users(.:format)      users#index 
         POST /users(.:format)      users#create 
       new_user GET /users/new(.:format)     users#new 
       edit_user GET /users/:id/edit(.:format)    users#edit 
        user GET /users/:id(.:format)     users#show 
         PATCH /users/:id(.:format)     users#update 
         PUT /users/:id(.:format)     users#update 
         DELETE /users/:id(.:format)     users#destroy 
edit_account_activation GET /account_activations/:id/edit(.:format) account_activations#edit 
     password_resets POST /password_resets(.:format)    password_resets#create 
    new_password_reset GET /password_resets/new(.:format)   password_resets#new 
    edit_password_reset GET /password_resets/:id/edit(.:format)  password_resets#edit 
     password_reset PATCH /password_resets/:id(.:format)   password_resets#update 
         PUT /password_resets/:id(.:format)   password_resets#update 
+0

有错字'edit_password_resets_url'可能是原因,并尝试指定操作,'方法:: post'。并且路由'post“password_resets /:id/edit”=>“password_resets#edit”'always expect and'id' so pass id to to edit_password_reset_url(id)' – Sontya 2015-02-23 07:20:38

+0

@Sontya edit_password_resets_url导致没有方法错误。更新了我的路线。 – ChangeAgent 2015-02-23 07:33:36

+0

你的'link_to'现在看起来如何? – Sontya 2015-02-23 07:44:49

回答

0

谢谢你们想通了这个问题。我没有在用户模型中正确更新密码重置摘要属性。

正确更新它,似乎正在工作!

相关问题