【问题标题】:Devise confirmation: No routes matches设计确认:没有路线匹配
【发布时间】:2015-03-16 16:49:02
【问题描述】:
  1. 用户已注册
  2. 用户收到确认邮件
  3. 用户点击user_confirmation_url(@resource, :confirmation_token => @token)
  4. 用户转到 /users/confirmation.39?confirmation_token=V-UwSF5qzCt8mBVAFuwK
  5. 收到此错误

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

我收到此错误:找不到用户控制器的“确认”操作

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

用户控制器

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

注册控制器

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)

【问题讨论】:

  • 你能把rake routes | grep confirmation的输出贴出来吗?
  • 好的,完成。问题已更新
  • 很可能您在路由器中的devise_for 调用之前定义了resources :users 或类似的东西,这会混淆Rails 路由生成。如果您发布完整的路由器,它将帮助我们识别它。
  • 我不确定您使用的是什么版本的设计,但不应该只是user_confirmation_url(:confirmation_token =&gt; @token)(没有@resource)吗?
  • 您找到解决方案了吗?我也面临同样的问题。

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


【解决方案1】:

这里的问题在于视图生成器。

您是否看到,在您的路径中,您收到了users/confirmation.39 作为您的确认路线... 39 被您的控制器解释为一种格式。如果您查看rake routes

,它不会期望用户 ID (@resource)

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

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

【讨论】:

  • @katafrakt 在上面调用它,这只是在详细说明这个问题,以防其他人需要提示!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-05
  • 2013-06-15
  • 1970-01-01
  • 1970-01-01
  • 2015-11-21
  • 2015-12-09
相关资源
最近更新 更多