【问题标题】:Rails Routing Error on Email Confirmation电子邮件确认时出现 Rails 路由错误
【发布时间】:2013-04-21 18:47:08
【问题描述】:

我是 Rails 新手,我正在尝试在注册时添加电子邮件确认。我目前收到此错误。

(对于任何冗长且易于理解答案的加分。)

路由错误

没有路由匹配 {:action=>"edit", :controller=>"email_activations", :id=>false}

config/routes.rb

LootApp::Application.routes.draw do
  get "password_resets/new"
  get "sessions/new"

  resources :users
  resources :sessions
  resources :password_resets
  resources :email_activations
  root to: 'static_pages#home'

app/mailers/user_mailer.rb

class UserMailer < ActionMailer::Base
    def registration_confirmation(user)
        @user = user
        mail(:to => user.email, :subject => "registered", :from => "alain@private.com")
    end
end

app/controllers/email_activations_controller.rb

class EmailActivationsController < ApplicationController
    def edit
        @user = User.find_by_email_activation_token!(params[:id])
        @user.email_activation_token = true
        redirect_to root_url, :notice => "Email has been verified."
    end
end

app/views/user_mailer/registration_confirmation.html.haml

请确认您的电子邮件地址!

= edit_email_activation_url(@user.email_activation_token)

【问题讨论】:

  • 使用 edit_email_activation_url(@user.email_activation_token) 生成什么 url?
  • 它甚至没有那么远。当我去完成注册时,它只会立即给我这个错误。没有发送电子邮件。

标签: ruby-on-rails ruby ruby-on-rails-3 confirmation-email


【解决方案1】:

rails routes中的resources关键字是一个神奇的关键字,默认创建7个restful路由

edit 就是其中之一

查看这些文档link http://guides.rubyonrails.org/routing.html#crud-verbs-and-actions

edit 期望编辑一条记录,因此需要一个 id 来查找要编辑的记录

在你的情况下

您可以在用户控制器中添加自定义操作

喜欢

在用户控制器中

  def accept_invitation
        @user = User.find_by_email_activation_token!(params[:token])
        @user.email_activation_token = true
        redirect_to root_url, :notice => "Email has been verified."
    end

在 routes.rb 中

   resources :users do
      collection do 
         get :accept_invitation
      end 
    end

在 app/views/user_mailer/registration_confirmation.html.haml

accept_invitation_users_url({:token=>@user.email_activation_token})

查看如何添加自定义路由here http://guides.rubyonrails.org/routing.html#adding-more-restful-actions

【讨论】:

  • 由于某种原因给了我这个错误 No route matches {:action=>"edit", :controller=>"password_resets", :id=>nil}
猜你喜欢
  • 2013-04-23
  • 2014-07-18
  • 1970-01-01
  • 1970-01-01
  • 2013-04-22
  • 2017-02-01
  • 2021-04-03
  • 2013-04-23
  • 2015-04-05
相关资源
最近更新 更多