Ruby-on-rails 设计可确认,如何在点击时重新发送确认电子邮件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9627564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Devise confirmable, how to resend a confirmation email on click?
提问by AnApprentice
I'm using devise confirmable. I want to give the user a link to click and resend the confirmation email. Problem is, when the user clicks the link, it isn't going to the devise controller. Is there something I'm missing in the routes.rb file? Here is my setup:
我正在使用可确认的设计。我想给用户一个链接,点击并重新发送确认电子邮件。问题是,当用户单击链接时,它不会转到设备控制器。是不是我在 routes.rb 文件中遗漏了什么?这是我的设置:
routes.rb
路由文件
devise_for :users, :controllers => { :registrations => "registrations", :sessions => "sessions", :omniauth_callbacks => "authentications" }
user.rb
用户名
devise :omniauthable, :database_authenticatable, :registerable, :confirmable, :recoverable, :rememberable, :trackable, :validatable
The view:
风景:
<a href="/users/confirmation/new" data-remote="true" data-method="post">Resend confirmation</a>
Thanks
谢谢
采纳答案by Rodrigo Flores
As you could see on https://github.com/plataformatec/devise/blob/master/app/controllers/devise/confirmations_controller.rb#L2, the HTTP method for confirmation#new is GET, not POST. Try to remove 'data-method="post"' and see if it works.
正如您在https://github.com/plataformatec/devise/blob/master/app/controllers/devise/confirmations_controller.rb#L2 上看到的那样,用于确认#new的 HTTP 方法是 GET,而不是 POST。尝试删除 'data-method="post"' 并查看它是否有效。
回答by MZaragoza
to send confirmation instructions to the user you find the user and then just user.send_confirmation_instructions
向您找到该用户的用户发送确认说明,然后只需 user.send_confirmation_instructions
namespace :user do
task :resend_confirmation => :environment do
users = User.where('confirmation_token IS NOT NULL')
users.each do |user|
user.send_confirmation_instructions
end
end
end
回答by bigsolom
resource_params
is a method defined at devise controller which gets the controller resources specific do device resource (User) for example. definition in DeviseController
是在设计控制器中定义的一种方法,例如,它获取控制器资源特定的设备资源(用户)。DeviseController 中的定义
def resource_params
params.fetch(resource_name, {})
end
so in order to pass the email as a parameter you neet to include it in a user hash, so in the view instead of
因此,为了将电子邮件作为参数传递,您需要将其包含在用户哈希中,因此在视图中而不是
link_to('resend', user_confirmation_path(email: "[email protected]"), :method => :post)
insert the email in a Hash
将电子邮件插入哈希
link_to('resend', user_confirmation_path(user: {:email => "[email protected]"}), :method => :post)
this way devise will pick up the email parameter
这种方式设计将拿起电子邮件参数
回答by Gabriel Osorio
Pretty old post. Though, instead of sending the instructions directly, you might just want to point the user to Devise's workflow:
很老的帖子了。但是,您可能只想将用户指向 Devise 的工作流程,而不是直接发送指令:
= link_to 'Resend confirmation', new_user_confirmation_path
That'll take the user to Devise's view requesting the email to send the confirmation instructions.
这会将用户带到 Devise 的视图,请求通过电子邮件发送确认说明。
Hope it helps anyone, anyway. :)
无论如何,希望它可以帮助任何人。:)
回答by benoitr
Here is my solution. Hope not missing something in my switch in resource_params. Note: not ajaxified
这是我的解决方案。希望我在 resource_params 中的开关中没有遗漏任何东西。注意:不是ajaxified
confirmation controller
确认控制器
class ConfirmationsController < Devise::ConfirmationsController
# POST /resource/confirmation
def create
# self.resource = resource_class.send_confirmation_instructions(resource_params)
self.resource = resource_class.send_confirmation_instructions({email: current_user.email})
if successfully_sent?(resource)
respond_with({}, :location => after_resending_confirmation_instructions_path_for(resource_name))
else
respond_with(resource)
end
end
protected
# The path used after resending confirmation instructions.
def after_resending_confirmation_instructions_path_for(resource_name)
root_path
end
end
routes
路线
devise_for :users, controllers: { confirmations: "confirmations" }
view
看法
<%= link_to "resend confirmation", user_confirmation_path, data: { method: :post } %>
回答by Karlo Smid
here is my solution that will open devise form where user can enter email address and request new email with confirmation token. All devise logic regarding email verification is preserved:
这是我的解决方案,它将打开设计表单,用户可以在其中输入电子邮件地址并使用确认令牌请求新电子邮件。保留有关电子邮件验证的所有设计逻辑:
app/controllers/confirmations_controller.rb
应用程序/控制器/confirmations_controller.rb
class ConfirmationsController < Devise::ConfirmationsController
# GET /resource/confirmation/new
def new
self.resource = resource_class.new
end
end
config/routes.rb
配置/路由.rb
devise_for :users, controllers: { confirmations: "confirmations" }
devise_for :users, controllers: { confirmations: "confirmations" }
app/views/devise/confirmations/new.html.erb
app/views/devise/confirmations/new.html.erb
<h2>Resend confirmation instructions</h2>
<%= form_for(resource, :as => resource_name, :url => confirmation_path(resource_name), :html => { :method => :post }) do |f| %>
<%= devise_error_messages! %>
<div><%= f.label :email %><br />
<%= f.email_field :email, :autofocus => true %></div>
<div><%= f.submit "Resend confirmation instructions" %></div>
<% end %>
<%= render "devise/shared/links" %>
回答by Lorenzo
I am sharing my solution as it's a bit of a different take, but closer to the normal user flow in my opinion (redirect to thanks page where you have a button to resend without writing email again), and you won't have to override the confirmations controller.
我正在分享我的解决方案,因为它有点不同,但在我看来更接近正常的用户流程(重定向到感谢页面,您可以在其中重新发送一个按钮而无需再次编写电子邮件),并且您不必覆盖确认控制器。
After signing up, the user get redirected to a 'Thanks' page. In registrations_controller:
注册后,用户被重定向到“谢谢”页面。在注册_控制器中:
def after_inactive_sign_up_path_for(resource)
session[:user_id] = resource.id
redirect_to thanks_url
end
in users controller, you just use the .send_confirmation_instructions on user
在用户控制器中,您只需对用户使用 .send_confirmation_instructions
def thanks
@user = User.find_by(id: session[:user_id])
end
def resend_confirmation
user = User.find_by(id: params[:user_id])
user.send_confirmation_instructions
end
routes:
路线:
get '/thanks', to: 'users#thanks'
post '/resend_confirmation', to: 'users#resend_confirmation', as: 'resend_confirmation'
finally, in the 'thanks' view:
最后,在“谢谢”的观点中:
<%= button_to "Resend confirmation", resend_confirmation_path(:user_id => @user.id) %>
This could be cleaned up a bit, I'm sure, as I've just wrote it and I'm still new at Rails, but I was looking for this kind of solutions on Stack and could not find it so I thought to share it.
这可以稍微清理一下,我敢肯定,因为我刚刚写了它,而且我还是 Rails 的新手,但是我在 Stack 上寻找这种解决方案却找不到它,所以我想分享它。
回答by coberlin
To resend the confirmation email, you want the post method with just 'users/confirmation'--no 'new' at the end.
要重新发送确认电子邮件,您需要仅包含“用户/确认”的 post 方法——末尾没有“新”。
Here's a form that requests the user's email and submits the confirmation resend request to Devise as follows:
这是一个请求用户电子邮件并将确认重新发送请求提交给 Devise 的表单,如下所示:
form_for(resource, url: user_confirmation_path) do |f|
.form-inputs
= f.input :email
.form-actions
= f.button :submit, "Resend"

