电子邮件确认中的Rails路由错误 [英] Rails Routing Error on Email Confirmation

查看:120
本文介绍了电子邮件确认中的Rails路由错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am new to rails and I am trying to add a email confirmation upon register. I currently get this error.

(任何详细且容易理解答案的奖励积分.)

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

No route matches {: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

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

Confirm your email address please!

= edit_email_activation_url(@ user.email_activation_token)

= edit_email_activation_url(@user.email_activation_token)

推荐答案

resources关键字是Rails路由中的一个神奇关键字,默认情况下会创建7条宁静的路由

resources keyword in rails routes is a magical keyword that creates 7 restful routes by default

edit是其中之一

检查这些文档链接 http://guides.rubyonrails.org/routing.html#crud-verbs和动作

edit希望编辑一条记录,因此需要一个ID才能找到要编辑的记录

edit expects to edit a record so requires a id to find the record for editing

以您的情况

您只需在用户控制器中添加自定义操作

you can just add a custom action in users controller

喜欢

在UsersController中

  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})

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

Check out how to add custom routes here http://guides.rubyonrails.org/routing.html#adding-more-restful-actions

这篇关于电子邮件确认中的Rails路由错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆