Why am I getting an an undefined method error when I try to add email confirmations in my Rails 4 app?

StackOverflow https://stackoverflow.com/questions/20456080

Question

I'm a newbie at Rails and I was hoping somebody could help me solve this little problem. I keep on getting an ActionView::Template::Error which says undefined method 'verify_email_users_url' for #<#<Class:0x68d5730>:0x6ac35c0> when I try to implement email confirmations into my Rails 4 app.

This is the relevant code for the feature:

app/controllers/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

app/models/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

app/views/user_mailer/registration_confirmation.text.erb

To confirm your email, click the URL below.

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

config/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

app/mailers/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

Here is the relevant test that is failing. How do I get this test to pass?

spec/mailers/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

If anyone could tell me where I'm going wrong that would be great. If you need any more information to understand what is going on here, please do no hesitate to ask.

Thank you.

Was it helpful?

Solution

Use collection:

  resources :users do
    member do
      get :following, :followers #(this line refers to some other methods)
    end
    collection do
      get :verify_email
    end
  end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top