当我尝试在我的Rails 4应用程序中添加电子邮件确认时,为什么会出现未定义的方法错误?

时间:2013-12-08 16:56:24

标签: ruby-on-rails ruby email-validation

我是Rails的新手,我希望有人可以帮我解决这个小问题。当我尝试在我的Rails 4应用中实施电子邮件确认时,我继续收到ActionView::Template::Error undefined method 'verify_email_users_url' for #<#<Class:0x68d5730>:0x6ac35c0>

这是该功能的相关代码:

应用程序/控制器/ users_controller.rb

  def create
    @user = User.new(user_params)
    if @user.save
      UserMailer.registration_confirmation(@user).deliver
      redirect_to root_url, notice: "A confirmation email has been sent to your inbox"
    elsif signed_in?
      redirect_to root_url
    else
      render 'new'
    end
  end

  def verify_email
    @user = User.find_by_email_token(params[:email_token])
    @user.email_confirmation_token = true
    @user.save
    redirect_to @user, notice: "Your email has been verified!"
  end

应用程序/模型/ user.rb

class User < ActiveRecord::Base

before_create :create_email_token

  def User.new_remember_token
    SecureRandom.urlsafe_base64
  end

  def User.encrypt(token)
    Digest::SHA1.hexdigest(token.to_s)
  end

  private
    def create_email_token
      self.email_token = User.encrypt(User.new_remember_token)
    end
end

应用程序/视图/ user_mailer文件/ registration_confirmation.text.erb

To confirm your email, click the URL below.

<%= verify_email_users_url(email_token: @user.email_token) %>

配置/ routes.rb中

SampleApp::Application.routes.draw do
  resources :users do
    member do
      get :following, :followers (this line refers to some other methods)
      get :verify_email
    end
  end
end

应用程序/邮寄者/ user_mailer.rb

class UserMailer < ActionMailer::Base
  default from: "money@example.com

  def registration_confirmation(user)
    @user = user
    mail to: @user.email, subject: "Please confirm your email"
  end
end

这是失败的相关测试。我如何让这个测试通过?

规格/邮寄者/ user_mailer_spec.rb

require "spec_helper"

describe UserMailer do
  let(:user) {FactoryGirl.create(:user, email_token: "fj4543")}
  let(:mail) {UserMailer.registration_confirmation(user)}

  it "should send an email confirming a user's email" do
    mail.subject.should eq("Please confirm your email")
    mail.to.should eq([user.email])
    mail.from.should eq(["money@example.com"])
    mail.body.encoded.should match(verify_email_users_path(email_token: user.email_token))
  end
end

如果有人能告诉我哪里出错了那会很棒。如果您需要更多信息来了解这里发生的事情,请毫不犹豫地询问。

谢谢。

1 个答案:

答案 0 :(得分:1)

使用集合:

  resources :users do
    member do
      get :following, :followers #(this line refers to some other methods)
    end
    collection do
      get :verify_email
    end
  end