ruby-on-rails - 设计确认 : No routes matches

标签 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. 它得到这个错误

enter image description here

如果我手动将 url 更改为: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

用户 Controller

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

注册 Controller

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)

最佳答案

这里的问题与 View 生成器有关。

你看到了吗,在你的路径中,你正在为你的确认路由获取 users/confirmation.39 ......你的 Controller 正在将 39 解释为一种格式。如果您看一下 rake routes

,它不需要用户 ID (@resource)

这里的答案是从电子邮件/ View 中的 url_helper 中删除 @resource,如果需要,将 ID 作为参数与 token 一起传递

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

关于ruby-on-rails - 设计确认 : No routes matches,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28011791/

相关文章:

ruby-on-rails - Rails:运行我的种子文件和Elasticsearch抛出:多个索引

ruby-on-rails - 如何在Rails中获取 "Application Trace"而不是完整的详细回溯?

ruby-on-rails - 设计如何在 View 中访问资源/资源名称

ruby-on-rails - 使用 Devise 登录后测试重定向

ruby-on-rails - 如何在 spree commerce 中创建主题

ruby-on-rails - 未定义的方法 `permit' 为 "titleofcoolstuff":String

ruby-on-rails - 未定义的方法 `each' 为 "#<Complaigns::ActiveRecord_Relation:0x00000003cfb4c8>":String

ruby-on-rails - 从您自己的 gem 创建路由条目

ruby-on-rails - rails : devise update user without password

ruby-on-rails - 有没有办法获取域的站点地图?