设计确认:没有路线匹配

时间:2015-01-18 16:04:51

标签: ruby-on-rails ruby-on-rails-4 devise

  1. 用户已注册
  2. 用户收到了确认电子邮件
  3. 用户点击 user_confirmation_url(@resource,:confirmation_token => @token)
  4. 用户访问/users/confirmation.39?confirmation_token=V-UwSF5qzCt8mBVAFuwK
  5. 出现此错误
  6. enter image description here

    如果我手动将网址更改为:users / confirmation / 39?confirmation_token = V-UwSF5qzCt8mBVAFuwK

    我收到此错误:操作'确认'找不到UsersController

    Routes.rb

          App::Application.routes.draw do
    
      get "pages/quickstart"
      get "pages/configuration"
      get "pages/invoices"
      get "/reports" => "reports#index" 
      get "/reports/historical_data" => "reports#historical_data", as: :historical_data
    
      #get "statements/document/month/:date"    => "statements#month", :document => true, as: :monthly_statements_document
      #get "statements/month/:date/" => "statements#month", as: :monthly_statements
    
      resources :reminders
      resources :reminder_users
      resources :forecasts
      resources :statements
      resources :monthly_statements
      resources :fuel_cards
      resources :accounts_payables
      resources :accounts_receivables
      resources :customers
      resources :invoices
      resources :suppliers
      devise_for :admin_users, ActiveAdmin::Devise.config
      ActiveAdmin.routes(self)
    
      devise_for :users, :controllers => { :registrations => "registrations"}
    
      # You can have the root of your site routed with "root"
      root 'pages#quickstart', as: :home
    
      get "/home" => "home#welcome"
      get "/registrations", to: redirect('/users/edit')
      get "/user", to: redirect('/users/edit')
      get "/user/change_password", as: :change_password
      match ':controller(/:action(/:id))(.:format)', via: [:get, :post]
    
    end
    

    (我用它来允许用户编辑他们的个人资料)

                  user_confirmation POST       /users/confirmation(.:format)                      devise/confirmations#create
              new_user_confirmation GET        /users/confirmation/new(.:format)                  devise/confirmations#new
                                    GET        /users/confirmation(.:format)                      devise/confirmations#show
    

    模型

    class User < ActiveRecord::Base
    
      devise :database_authenticatable, :registerable, :confirmable,
             :recoverable, :rememberable, :trackable, :validatable
    

    UserController中

    class UsersController < ApplicationController
    
      before_filter :authenticate_user!
    
      def edit_password
        @user = current_user
      end
    
      def update_password
        @user = User.find(current_user.id)
        if @user.update(user_params)
          # Sign in the user by passing validation in case his password changed
          sign_in @user, :bypass => true
          redirect_to root_path
        else
          render "edit"
        end
      end
    
      private
    
      def user_params
        # NOTE: Using `strong_parameters` gem
        params.required(:user).permit(:password, :password_confirmation)
      end
    end
    

    这个RegistrationController

    class RegistrationsController < Devise::RegistrationsController
      def update
        @user = User.find(current_user.id)
    
        successfully_updated = if needs_password?(@user, params)
          @user.update_with_password(devise_parameter_sanitizer.sanitize(:account_update))
        else
          # remove the virtual current_password attribute
          # update_without_password doesn't know how to ignore it
          params[:user].delete(:current_password)
          @user.update_without_password(devise_parameter_sanitizer.sanitize(:account_update))
        end
    
        if successfully_updated
          set_flash_message :notice, :updated
          # Sign in the user bypassing validation in case his password changed
          sign_in @user, :bypass => true
          #redirect_to after_update_path_for(@user)
          redirect_to edit_user_registration_path
        else
          render "edit"
        end
      end
    
      private
    
      # check if we need password to update user data
      # ie if password or email was changed
      # extend this as needed
      def needs_password?(user, params)
        user.email != params[:user][:email] ||
          params[:user][:password].present?
      end
    end
    

    设计(3.4.1)

1 个答案:

答案 0 :(得分:0)

这里的问题是视图生成器。

您是否看到,在您的路径中,您的确认路线正在users/confirmation.39 ...您的控制器将39解释为格式。如果您查看@resource

,则不会产生用户ID(rake routes

这里的答案是从电子邮件/视图中的@resource中删除url_helper,如果需要,将ID作为参数传递给令牌

user_confirmation_url(confirmation_token: @token, id: @resource)