Question

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

(Bonus points for any verbose and easily understood answer.)

Routing Error

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)

Was it helpful?

Solution

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

edit is one of those

check these docs link http://guides.rubyonrails.org/routing.html#crud-verbs-and-actions

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

in your case

you can just add a custom action in users controller

like

in 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

in routes.rb

   resources :users do
      collection do 
         get :accept_invitation
      end 
    end

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

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

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top