Rails routing error during email confirmation

I am new to rails and I am trying to add confirmation email when registering. I am currently getting this error.

( Bonus points for any detailed and easy to understand answer.)

Routing error

There are no route matches {: action => "edit",: controller => "email_activations",: id => false}

configurations /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' 

application / senders / 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 

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

Verify your email address!

= edit_email_activation_url (@ user.email_activation_token)

+1
source share
1 answer

resources keyword in rails routes is a magic keyword that by default creates 7 calm routes.

edit is one of those

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

edit is waiting for an entry to be edited, so an identifier is required to search for an entry

in your case

you can just add a custom action to the user controller

as

in usercontroller

  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 the application / views / user _mailer / registration_confirmation.html.haml

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

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

+2
source

Source: https://habr.com/ru/post/1233266/


All Articles