将params传递给form_for中的link_to(或如何通过短信重置设计密码)

时间:2016-11-26 20:18:24

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

我页面上的用户可以使用他们的电话号码进行注册。如果他们忘记了密码,应该通过短信重新发送。

我的目标是在我的registrations / new.html.haml页面上有一个链接,它会触发一些自定义控制器(发送带密码的短信)。

我想要替换Devise的passwords_controller.rb编辑操作,但似乎没有触发:

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

  end
end

- &GT; “Hello”永远不会出现在日志中。

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

现在,我的sessions / new看起来像这样:

=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'

所以,基本上我现在需要的是以某种方式将:phone param从这个表单传递给link_to helper。我试过googling“rails pass params to link_to”的所有结果,但是在调用我的profile_controller时

class Front::ProfileController < FrontController

    def change_password
        logger.debug "This is Patrick with params: #{params}"
    end
end

Params不包含电话号码。如何通过params或其他方式实现所需的功能?

2 个答案:

答案 0 :(得分:0)

你需要这样的东西吗?

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

此外,Devise使用BCrypt加密密码。 BCrypt是一个散列函数,因此,你无法获得真正的密码。如果需要,您可以重新生成新密码并通过短信发送新密码,但是您无法发送旧的真实密码。

答案 1 :(得分:0)

搜索解决方案让我this SO question,这就是我最终解决问题的方法。

我的最终表格代码:

=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'

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

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