设计确认,如何在点击时重新发送确认电子邮件?

时间:2012-03-09 01:06:58

标签: ruby-on-rails ruby-on-rails-3 devise

我正在使用设计确认。我想给用户一个链接,点击并重新发送确认电子邮件。问题是,当用户点击链接时,它不会进入设计控制器。在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

观点:

<a href="/users/confirmation/new" data-remote="true" data-method="post">Resend confirmation</a>

由于

9 个答案:

答案 0 :(得分:28)

向用户发送确认说明,然后发送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

答案 1 :(得分:7)

resource_params

是在设备控制器中定义的方法,例如,获取特定于控制器资源的设备资源(用户)。 DeviseController中的定义

def resource_params
params.fetch(resource_name, {})
end

因此,为了将电子邮件作为参数传递,您需要将其包含在用户哈希中,因此在视图中而不是

link_to('resend', user_confirmation_path(email: "user@example.com"), :method => :post)                    

将电子邮件插入哈希

link_to('resend', user_confirmation_path(user: {:email => "user@example.com"}), :method => :post)

这种方式设计将获取电子邮件参数

答案 2 :(得分:6)

正如您在https://github.com/plataformatec/devise/blob/master/app/controllers/devise/confirmations_controller.rb#L2上看到的,确认#new的HTTP方法是GET,而不是POST。尝试删除'data-method =“post”'并查看它是否有效。

答案 3 :(得分:4)

很老的帖子。但是,您可能只想将用户指向设计工作流程,而不是直接发送说明:

= link_to 'Resend confirmation', new_user_confirmation_path

将用户带到设计视图,要求电子邮件发送确认说明。

无论如何,希望它对任何人都有帮助。 :)

答案 4 :(得分:3)

这是我的解决方案。希望不要错过resource_params中我的交换机中的内容。 注意:不是ajaxified

确认控制器

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

路由

devise_for :users, controllers: { confirmations: "confirmations" } 

视图

<%= link_to "resend confirmation", user_confirmation_path, data: { method: :post } %>

答案 5 :(得分:2)

这是我的解决方案,它将打开设计表单,用户可以输入电子邮件地址并请求带有确认令牌的新电子邮件。所有关于电子邮件验证的设计逻辑都会保留:

应用程序/控制器/ confirmations_controller.rb

class ConfirmationsController < Devise::ConfirmationsController
# GET /resource/confirmation/new
  def new
    self.resource = resource_class.new
  end
end

配置/ routes.rb中

devise_for :users, controllers: { confirmations: "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" %>

答案 6 :(得分:2)

要重新发送确认电子邮件,您希望post方法只包含'users / confirmation' - 最后不要'new'。

这是一个请求用户电子邮件的表单,并将确认重发请求提交给Devise,如下所示:

form_for(resource, url: user_confirmation_path) do |f|
  .form-inputs
    = f.input :email
  .form-actions
    = f.button :submit, "Resend"

答案 7 :(得分:2)

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

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.

答案 8 :(得分:1)

您可能希望将其安排在每晚运行,这样您就不需要观看它了。

这是一篇关于如何做的文章

http://www.cheynewallace.com/resend-devise-confirmation-emails-for-incomplete