我是rails的新手,我正在尝试在注册时添加电子邮件确认。我目前收到此错误。
(奖励积分 任何详细及易于理解的答案。)
没有路线匹配{:action =>"编辑",:controller =>" email_activations",:id => false}
配置/ 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'
应用/邮寄者/ 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
应用/控制器/ 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
应用/视图/ user_mailer文件/ registration_confirmation.html.haml
请确认您的电子邮件地址!
= edit_email_activation_url(@ user.email_activation_token)
答案 0 :(得分:2)
resources
关键字是一个神奇的关键字,默认情况下会创建7条静止路由
edit
就是其中之一
检查这些文档link http://guides.rubyonrails.org/routing.html#crud-verbs-and-actions
编辑期望编辑记录,因此需要id才能找到要编辑的记录
在你的情况下
您只需在用户控制器中添加自定义操作
即可像
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
在routes.rb中
resources :users do
collection do
get :accept_invitation
end
end
app / views / user_mailer / registration_confirmation.html.haml 中的
accept_invitation_users_url({:token=>@user.email_activation_token})
查看如何添加自定义路由here http://guides.rubyonrails.org/routing.html#adding-more-restful-actions