Rails 6:从“忘记密码”链接更新密码时出现路由错误

时间:2019-11-23 14:16:57

标签: ruby-on-rails ruby-on-rails-5 ruby-on-rails-6

没有路由与[PATCH]“ /passwords.Cl8LgygMOkPelyqCSoraMQ ”匹配,这是错误的,我尝试使用忘记密码链接来更新密码。

如果我手动去修复路由路径没有路由与[PATCH]“ / passwords / Cl8LgygMOkPelyqCSoraMQ ”匹配,则会显示空白页面。

我认为路径中不应存在'。。我认为应该用'/'代替路线。但是后来我从这里知道了。来自。

routes.rb

Rails.application.routes.draw do
  root 'posts#index'
  resources :users
  resources :posts 
  resource :sessions, only: [:new, :create, :destroy]
  resources :passwords, only: [:new, :create, :edit, :update]

end

PasswordController.rb

class PasswordsController < ApplicationController
   def new
   end

   def create
      user = User.find_by_email(params[:email])
      if user 
         user.send_password_reset
         flash[:success] = "Email Sent"
         redirect_to root_url  
      else
         flash.now[:danger] = "Email address not found"
         render 'new'
      end
    end


    def edit
      @user = User.find_by_reset_password_token!(params[:id])
    end


    def update
      @user =  User.find_by_reset_password_token!(params[:id])
      if @user.reset_password_sent_at < 2.hours.ago
         flash[:notice] = 'Password reset has expired'
         redirect_to new_password_path
      elsif @user.params(user_params)
         flash[:notice] = "Password has been reset"
         redirect_to new_sessions_path
      else
         render 'edit'
      end
    end

    private

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


end

密码-edit.html.erb

<div class="row">
   <div class="col-md-12" style="align: center">
     <h1>Reset your password</h1>
   </div>
   <div class="col-md-offset-3 col-md-9">
    <% if @user.errors.any? %>
        <div class="error_messages">
          <h2>Form is invalid</h2>
          <ul>
            <% @user.errors.full_messages.each do |msg| %>
                <li><%= msg %></li>
            <% end %>
        </ul>
        </div>
    <% end %>
   </div>
   <div class="col-md-offset-3 col-md-9">
     <%= form_for @user,  url: passwords_path(params[:id]) do|f| %>
        <div class="form-group">
            <%= f.label :password, "New Password" %>
            <%= f.text_field :password, class: "form-control" %>
        </div>
        <div class="form-group">
            <%= f.label :confirm_password, "Confirm New Password" %>
            <%= f.text_field :password_confirmation, class: "form-control" %>
        </div>
        <%= f.submit "Change Password", class: "btn btn-primary btn-block" %>
     <% end %>
   </div>
</div>

enter image description here

0 个答案:

没有答案