2017-10-21 11 views
1

ユーザーがログイン画面で[パスワードを忘れた場合]をクリックすると、ルート/ 'パスワードのリセット'にリダイレクトされます。今、私はTwilioからコードを受信して​​SMSを受け取るためにあなたの電子メールを入力するためのフォームを正しく作成する方法を理解しようとしています。Twilio SMSパスワード忘れたレール

<div class="form-group", style="width:50%;"> 
    <%= form_for @user, url: password_patch_path(current_user) do |f| %> 
    <div class="form-group"> 
     <%= f.label :email %> 
     <%= f.email_field :email, class: "form-control" %> 
    </div> 
    <%= f.submit "Get Confirmation Code", class: "btn btn-default" %> 
    <% end %> 
</div> 

私が問題になっているのは、@userはnilであり、フォームの冒頭にURLが正しいかどうかは不明です。誰もログインしていないので、@userはnilだから意味があります。だから私はそれが何であるべきか分かりません。

私のルートは

get '/password-reset', :to => 'passwords#edit', as: :password_reset 
post '/password-reset', :to => 'passwords#reset', as: :password_edit 
patch '/password-confirmation', :to => 'passwords#update', as: :password_patch 

であり、私のパスワードコントローラは、ユーザーが彼/彼女のパスワードを忘れた場合は何もcurrent_userがないことを右です

class PasswordsController < ApplicationController 
before_action :authenticated?, only: [:edit, :update] 

def reset 
ConfirmationSender.send_confirmation_to(current_user) 
redirect_to new_confirmation_path 
end 

def edit 
@user = current_user 
end 

def update 
    if passwords_not_empty? && passwords_equal? 
    current_user.update(password_params) 
    redirect_to users_dashboard_path(current_user.username), success: "Password Updated" 
    session[:authenticated] = false 
    else 
    redirect_to password_edit_path(current_user.username), warning: "Error, please try again." 
    end 
end 

private 

    def password_params 
    params.require(:user).permit(:password, :password_confirmation) 
    end 

    def passwords_not_empty? 
    params[:user][:password].length > 0 && params[:user][:password_confirmation].length > 0 
    end 

    def passwords_equal? 
    params[:user][:password] == params[:user][:password_confirmation] 
    end 

    def authenticated? 
    render :file => "#{Rails.root}/public/404.html", :status => 404 unless session[:authenticated] 
    end 
end 

答えて

1

のように見えます。次のように私が設計し直します:

PasswordsContoller

class PasswordsController < ApplicationController 
    before_action :authenticated?, only: [:update] 

    def reset 
    @user = User.find_by(email: params[:user][:email]) 
    if @user.present? 
     ConfirmationSender.send_confirmation_to(@user) 
     redirect_to new_confirmation_path 
    else 
     redirect_to password_reset_path, warning: "Email not found." 
    end 
    end 

    def edit 
    @user = User.new 
    end 

... 

end 

フォーム

<div class="form-group", style="width:50%;"> 
    <%= form_for @user, url: password_edit_path do |f| %> 
    <div class="form-group"> 
     <%= f.label :email %> 
     <%= f.email_field :email, class: "form-control" %> 
    </div> 
    <%= f.submit "Get Confirmation Code", class: "btn btn-default" %> 
    <% end %> 
</div> 

新しいedit方法種子空白のユーザーを持つフォーム。新しいresetメソッドは、電子メールでユーザーを検索し、ユーザーが見つかった場合にトークンを送信します。そうでない場合、フラッシュメッセージが見つからない電子メールが表示され、忘れたパスワードフォームにリダイレクトされます。

これにより、パスワード確認を要求するための正しいパスがフォームに使用されます。

+0

ありがとうTom!本当に助けていただければ幸いです。 – Sam

+0

問題ありません。あなたが有益である投票の回答を恐れることはありません。 –

関連する問題