2016-11-26 10 views
1

我的網頁上的用戶可以註冊他們的電話號碼。如果他們忘記了密碼,應該通過短信重新發送。將參數傳遞給link_to內的form_for(或如何重置通過短信設計密碼)

我的目標是在我的registrations/new.html.haml頁面上有一個鏈接,它會觸發一些自定義控制器(使用密碼發送SMS)。

我想更換設計的passwords_controller.rb編輯動作,但它似乎並沒有被觸發:

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

    end 
end 

- >「你好」永遠不會出現在日誌中。

之後,我創建了一個自定義控制器「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' 

所以,基本上我需要什麼權現在是以某種方式將這個表單中的phone參數傳遞給link_to helper。我試圖從谷歌上搜索「軌通PARAMS到LINK_TO」所有的結果,但打電話給我profile_controller時

class Front::ProfileController < FrontController 

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

PARAMS不包含電話號碼。我怎樣才能達到通過PARAMS或其他所需的功能?

回答

0

你需要這樣的東西嗎?

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

此外,設計使用BCrypt加密密碼。 BCrypt是一個哈希函數,因此,你不能得到真正的密碼。如果需要,您可以重新生成新密碼並通過短信發送新密碼,但無法發送舊的真實密碼。

+0

是的,我需要這樣的事情,但我怎樣提取'f.text_field:phone' - 價值從形式? – mohnstrudel

+0

該模型有字段':phone'?如果是的話,那應該工作,如果沒有,你應該使用'text_field_tag'手機',@ phone_value' –

+0

我改變了我的link_to到 '= link_to'通過短信發送密碼',reset_password_path(@user,phone:params [:phone ]),方法:: get' 但仍然沒有收到我參數中的參數hash 'params =>「get」,「authenticity_token」=>「ze8PgJinJuiIsROfAHca + fU + Owp0IKtXBkJw0R1ik6f + S0E2zB4aOvj5jdq8CGkwib8IlN8RascVIFZdIdJqFw ==「,」controller「=>」front/profile「,」action「=>」change_password「}允許:false>' – mohnstrudel

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