将params传递到link_to内部form_for(或如何通过SMS重置密码)



我页面上的用户可以用他们的电话号码注册。如果他们忘记了密码,应该通过短信感到不满。

我的目标是在我的注册/new.html.haml页面上进行链接,该链接将触发某些自定义控制器(该自定义控制器(它带有密码发送SMS)。

我想到更换Deaise的passwords_controller.rb编辑操作,但似乎没有触发:

class Front::Users::PasswordsController < Devise::PasswordsController
  # GET /resource/password/new
  def new
    p "hello"
    super
  end
end

->" Hello"永远不会出现在日志中。

之后,我用路由创建了一个自定义控制器" profile_controller.rb",如下所示: get 'reset_password', to: 'front/profile#change_password'

现在,我的会议/新外观如下:

=form_for(resource, as: resource_name, url: session_path(resource_name)) do |f|
    .section.section-inset-1
        .container
            .row
                .col-xs-12.col-md-8.col-md-offset-2
                    %section
                        %h4 Sign in
                        .rd-mailform.row.flow-offset-6
                            .col-xs-12.col-sm-6
                                =f.text_field :phone, placeholder: "Phone number", data: { constraints: "@NotEmpty" }, autofocus: true, id: "standard_order_phone"
                            .col-xs-12.col-sm-6
                                =f.password_field :password, placeholder: "Password", data: { constraints: "@Password" }, class: "form-input", autocomplete: "off" 
                            .col-xs-12.col-sm-6.text-left
                                %label.mfCheckbox
                                    =f.check_box :remember_me, class: 'mfCheckbox__input'
                                    %span Remember me
                            .col-xs-12.col-sm-6.text-right
                                %label.mfCheckbox#send_password
                                    =link_to 'Send password via SMS', reset_password_path(phone: params["user[phone]"]), method: :get
                    %section.section-inset-3
                        .row.offset-5
                            .col-xs-12
                                =f.submit 'Sign in', class: 'btn btn-sm btn-min-width btn-success'
        %section.section-inset-3
            .row.ud_mt_4em
                %h6 Not signed up yet?
            .row.offset-5
                =link_to 'Sign up', new_user_registration_path, method: :get, class: 'btn btn-sm btn-min-width btn-success-mod-1'

因此,基本上我现在需要的是以某种方式将以下方式传递给link_to助手。我尝试了谷歌搜索" rails将参数传递给link_to"的所有结果,但是当调用我的个人资料_controller

class Front::ProfileController < FrontController
    def change_password
        logger.debug "This is Patrick with params: #{params}"
    end
end

参数不包含电话号码。如何实现传递参数或否则请使用所需功能?

您需要这样的东西吗?

link_to 'Send me the pass', send_pass_path(@user, param: 'foo')

此外,设计使用BCrypt加密密码。BCRypt是一个哈希功能,因为那是您无法获得真正的密码。您可以再生一个新密码,如果需要,可以通过SMS发送新密码,但是您不能发送旧的真实通行证。

搜索解决方案使我遇到了问题,这就是我最终解决问题的方式。

我的最终表格代码:

=form_for(resource, as: resource_name, url: session_path(resource_name)) do |f|
    .section.section-inset-1
        .container
            .row
                .col-xs-12.col-md-8.col-md-offset-2
                    %section
                        %h4 Sign in
                        .rd-mailform.row.flow-offset-6
                            .col-xs-12.col-sm-6
                                =f.text_field :phone, placeholder: "Phone number", data: { constraints: "@NotEmpty" }, autofocus: true, id: "standard_order_phone"
                            .col-xs-12.col-sm-6
                                =f.password_field :password, placeholder: "Password", data: { constraints: "@Password" }, class: "form-input", autocomplete: "off" 
                            .col-xs-12.col-sm-6.text-left
                                %label.mfCheckbox
                                    =f.check_box :remember_me, class: 'mfCheckbox__input'
                                    %span Remember me
                            .col-xs-12.col-sm-6.text-right
                                #send_password
                                    =submit_tag 'Send password per SMS', class: "reset_password", id: 'reset_btn', name: 'reset', remote: true
                    %section.section-inset-3
                        .row.offset-5
                            .col-xs-12
                                =f.submit 'Sign in', class: 'btn btn-sm btn-min-width btn-success'
        %section.section-inset-3
            .row.ud_mt_4em
                %h6 No account yet?
            .row.offset-5
                =link_to 'Create one!', new_user_registration_path, method: :get, class: 'btn btn-sm btn-min-width btn-success-mod-1'

i然后扩展设计会话控制器:

class Front::Users::SessionsController < Devise::SessionsController

# before_action :configure_sign_in_params, only: [:create]
  # GET /resource/sign_in
  def new
    if params[:reset]
      phone = params[:user][:phone]
      # logger.debug("Phone is: #{phone}")
      new_password = Array.new
      5.times do
        new_password << rand(9)  
      end
      password = new_password.join("")
      User.where(phone: phone).update(password: password)
      stripped_phone = phone.gsub(/s+/, "").gsub(/[()]/, "").gsub(/-/, "")
      encoded_phone = URI.escape(stripped_phone)
      encoded_message = URI.escape("Your new password - #{password}.")
      # logger.debug("New password for user #{phone} - #{password}")
      # Some code for sending SMS
      # doc = Nokogiri::HTML(open(url))
      flash[:success] = "New password sent per SMS."
      redirect_to new_user_session_path
    else
      super
    end
  end
end

最新更新