Rails Routing Error on Email Confirmation

独自空忆成欢 提交于 2019-12-11 09:18:56

问题


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)


回答1:


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



来源:https://stackoverflow.com/questions/16256004/rails-routing-error-on-email-confirmation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!