1

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

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

路由错误

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

配置/路由.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'

应用程序/邮件程序/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

应用程序/控制器/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)

4

1 回答 1

2

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

edit是其中之一

检查这些文档链接 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

在路线.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})

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

于 2013-04-27T19:47:38.987 回答