【问题标题】:Custom variables in Devise reset password instructions?设计中的自定义变量重置密码说明?
【发布时间】:2012-07-17 12:48:27
【问题描述】:

我需要能够自定义 rails devise 邮件程序视图以获取重置密码说明。

为此,我需要做两件事。

  1. 为链接指定自定义 URL,使其成为基于特定业务逻辑的主机/域。这个主机和域来自浏览器中的URL,即用户点击忘记密码时的请求对象。所以我在delayed_job 中没有请求对象来根据需要处理它,因此我需要能够在发送电子邮件的delayed_job 中的某个时间点执行此操作。

  2. 将自定义变量传递给邮件视图,以便我可以为视图添加各种其他逻辑,根据需要隐藏和显示位。

有人可以帮忙吗?我可以看到您可以为设计生成邮件视图,但我还需要能够将各种项目传递给它。例如,我是否需要以某种方式自己覆盖用户模型和密码控制器中的功能?

【问题讨论】:

    标签: ruby-on-rails devise mailer password-recovery


    【解决方案1】:

    覆盖整个控制器方法并在send_reset_password_instructions opts 参数中添加参数将修复它。

    @resource.send_reset_password_instructions(
        email: @email,
        provider: 'email',
        redirect_url: @redirect_url,
        client_config: params[:config_name],
        parameter_passed: params[:parameter_passed],
      )
    

    您可以通过message['parameter_passed']访问视图中的参数

    【讨论】:

      【解决方案2】:

      所以,经过一番折腾、搜索和破解……这是不可能的。所以我最终编写了自己的邮件程序并绕过控制器中的设计重置密码方法,生成我自己的重置令牌,设置我需要的变量,调用我的 usermailer....并将设计 URL 嵌入到我的邮件中以获取它单击密码重置链接后,返回调用设计,然后一切都很好....

      我讨厌必须重写逻辑,但最终它是最快和最干净的解决方案。

      一种几乎奏效的方法是在我的用户模型上使用非 activerecord 属性来存储我需要的位并将其“破解”到设计视图中的 @resource 中,但这样做会造成一些麻烦,结果,我选择了上面的选项...

      【讨论】:

        【解决方案3】:

        我需要添加一个source 以包含在重置密码视图中,这是我实现的:

        class User < ActiveRecord::Base
          prepend ResetPasswordWithSource
        
          devise :recoverable
        
          ....
        end
        
        module User::ResetPasswordWithSource
          def send_reset_password_instructions(source=nil)
            @source = source
            super()
          end
        
          def send_devise_notification(notification, *args)
            args.last.merge!({ source: @source })
            super
          end
        end
        

        您可以从这里拨打user.send_reset_password_instructions('special_source')

        并且可以通过@options[:source] = 'special_source'在视图中访问

        【讨论】:

          【解决方案4】:

          在我意识到在调用 super 之前声明自定义变量会起作用之前,我也为此苦苦挣扎。

            def reset_password_instructions(record, token, opts={})
             @custom_variable = "Greetings, world"
          
              # your gorgeous code
          
             mailer_object = super
          
             mailer_object
            end
          

          【讨论】:

            【解决方案5】:

            您只需要添加一个flag 即可在视图邮件中显示。从这里你可以调用一个方法并传递参数。

            @user.send_reset_password_instructions("true")
            

            现在覆盖方法 send_reset_password_instructions

            def send_reset_password_instructions(option = nil)
              token = set_reset_password_token
              send_reset_password_instructions_notification(token, option)
              token 
            end
            
            def send_reset_password_instructions_notification(token, option = nil)
              send_devise_notification(:reset_password_instructions, token, :option => option)
            end
            

            然后您可以使用以下方式访问参数:

            message[:option]
            

            【讨论】:

              猜你喜欢
              • 2013-10-18
              • 2020-07-13
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2014-01-17
              • 2012-06-22
              • 1970-01-01
              • 2016-12-22
              相关资源
              最近更新 更多