【问题标题】:Devise confirmable, how to resend a confirmation email on click?设计可确认,如何在点击时重新发送确认电子邮件?
【发布时间】:2012-03-09 01:06:58
【问题描述】:

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

谢谢

【问题讨论】:

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


    【解决方案1】:

    要向用户发送确认说明,请找到该用户,然后只需 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
    

    【讨论】:

    • 小心!这将每天提醒您所有未确认的用户(如果您每天运行它),这可能会给您带来法律麻烦,因为他们中的一些人可能没有自己注册或者可能想要一个人呆着。如果每天运行,请输入where("created_at &gt; ?", Time.now - 72.hours).where("created_at &lt; ?", Time.now - 48.hours)(即仅发送给昨天注册的用户),否则您将发送垃圾邮件。您不想将其发送给今天注册的人,因为它会重新生成确认令牌,从而使第一封电子邮件无效。
    • 我用User.where('confirmed_at IS NULL')
    【解决方案2】:
    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)
    

    这样devise会获取email参数

    【讨论】:

      【解决方案3】:

      我正在分享我的解决方案,因为它有点不同,但在我看来更接近正常的用户流程(重定向到感谢页面,你有一个按钮可以重新发送而无需再次写电子邮件),你不会必须覆盖确认控制器。

      注册后,用户会被重定向到“感谢”页面。在registrations_controller:

        def after_inactive_sign_up_path_for(resource)
         session[:user_id] = resource.id
         redirect_to thanks_url
        end
      

      在用户控制器中,您只需对用户使用 .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
      

      路线:

      get '/thanks', to: 'users#thanks'
      post '/resend_confirmation', to: 'users#resend_confirmation', as: 'resend_confirmation'
      

      最后,在“感谢”视图中:

      <%= button_to "Resend confirmation", resend_confirmation_path(:user_id => @user.id) %>
      

      我敢肯定,这可以稍微清理一下,因为我刚刚写了它,而且我还是 Rails 的新手,但我在 Stack 上寻找这种解决方案,但找不到,所以我想分享一下。

      【讨论】:

        【解决方案4】:

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

        【讨论】:

        【解决方案5】:

        相当老的帖子。不过,您可能只想将用户指向 Devise 的工作流程,而不是直接发送说明:

        = link_to 'Resend confirmation', new_user_confirmation_path
        

        这会将用户带到 Devise 的视图,请求电子邮件发送确认说明。

        无论如何,希望它可以帮助任何人。 :)

        【讨论】:

        • 记得把"_user_"改成你的资源名
        【解决方案6】:

        这是我的解决方案。希望我的资源参数中的开关不会丢失任何东西。 注意:没有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 } %>
        

        【讨论】:

          【解决方案7】:

          要重新发送确认电子邮件,您希望 post 方法只包含“用户/确认”--末尾没有“新”。

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

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

          【讨论】:

            【解决方案8】:

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

            app/controllers/confirmations_controller.rb

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

            config/routes.rb

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

            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" %>
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2012-03-09
              • 2019-01-21
              • 1970-01-01
              • 2022-01-03
              • 2016-11-02
              • 1970-01-01
              相关资源
              最近更新 更多