2015-02-23 200 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

謝謝你們想通了這個問題。我沒有在用戶模型中正確更新密碼重置摘要屬性。

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

相關問題