0

パスワードリセットオプションを提供できるように、アプリにRailscast#274を実装しようとしています。パスワードリセットのフォームにメールを入力して、パスワードをリセットするためのリンクが記載されたメールを受信できるようになりました。新しいパスワードを入力して保存しようとすると、問題が発生し始めます。私はで終わったAction Controller:Exception caught。password_resetリンクを記載したメールを自分に送信した後のログの表示は次のとおりです。

Started GET "/password_resets/new" for 127.0.0.1 at 2012-01-26 00:50:42 -0500
  Processing by PasswordResetsController#new as HTML

password_resetリンクをクリックします。

Started GET "/password_resets/qlslPnuOhdyMCNseMnV3bA/edit" for 127.0.0.1 at 2012-01-26 00:51:08 -0500
  Processing by PasswordResetsController#edit as HTML
  Parameters: {"id"=>"qlslPnuOhdyMCNseMnV3bA"}

新しい:passwordと:password_confirmationを追加すると、次のエラーが発生します。

Started POST "/password_resets/qlslPnuOhdyMCNseMnV3bA" for 127.0.0.1 at 2012-01-26 00:53:08 -0500
  Processing by PasswordResetsController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"2egfT2lr35FhuVPWDB72vcS2zPlqC75tcyctRp61ZHw=", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "commit"=>"Update Password", "id"=>"qlslPnuOhdyMCNseMnV3bA"}

Started GET "/profiles/qlslPnuOhdyMCNseMnV3bA" for 127.0.0.1 at 2012-01-26 00:53:09 -0500
  Processing by ProfilesController#show as HTML
  Parameters: {"id"=>"qlslPnuOhdyMCNseMnV3bA"}

  Profile Load (0.9ms)  SELECT "profiles".* FROM "profiles" WHERE "profiles"."id" = 'qlslPnuOhdyMCNseMnV3bA' LIMIT 1
PGError: ERROR:  invalid input syntax for integer: "qlslPnuOhdyMCNseMnV3bA"
LINE 1: ...ofiles".* FROM "profiles" WHERE "profiles"."id" = 'qlslPnuOh...
                                                             ^
: SELECT  "profiles".* FROM "profiles" WHERE "profiles"."id" = 'qlslPnuOhdyMCNseMnV3bA' LIMIT 1
Completed   in 57ms

ActiveRecord::StatementInvalid (PGError: ERROR:  invalid input syntax for integer: "qlslPnuOhdyMCNseMnV3bA"
LINE 1: ...ofiles".* FROM "profiles" WHERE "profiles"."id" = 'qlslPnuOh...
                                                         ^
: SELECT  "profiles".* FROM "profiles" WHERE "profiles"."id" = 'qlslPnuOhdyMCNseMnV3bA' LIMIT 1):
  app/controllers/profiles_controller.rb:41:in `show'

のprofiles_controller.rb:41 show

def show
  @profile = Profile.find(params[:id])
  @user = User.find(@profile.user_id)
  ..
end

これを行う前に、データベースをダンプして実行しrake:db create、次にデータベースをrake:db migrate再シードしました。これは、既存のユーザーにpassword_reset_tokenを与えるスクリプトを実行していないことが原因でしょうか?

更新:含むpassword_resets_controller.rb

class PasswordResetsController <ApplicationController def new end

  def create
    @user = @User.find_by_email(params[:email])
    if user
      user.send_password_reset
      redirect_to new_password_reset_path, :notice => "Check your email for password reset instructions."
    else
      redirect_to new_password_reset_path, :notice => "Sorry, we couldn't find that email. Please try again."
    end
  end

  def edit
    @user = User.find_by_password_reset_token!(params[:id])
  end

  def update
    @user = User.find_by_password_reset_token!(params[:id])
    if @user.password_reset_sent_at < 2.hours.ago
      redirect_to new_password_reset_path, :alert => "Your password reset link has expired."
    elsif @user.update_attributes(params[:user])
      redirect_to profile_path, :notice => "Great news: Your password has been reset."
    else
      render :edit
    end
  end
end
4

1 に答える 1

1

問題はあなたにあるようPasswordResetsController#updateです:

def update
  @user = User.find_by_password_reset_token!(params[:id])
  if @user.password_reset_sent_at < 2.hours.ago
    redirect_to new_password_reset_path, :alert => "Your password reset link has expired."
  elsif @user.update_attributes(params[:user])
    redirect_to profile_path, :notice => "Great news: Your password has been reset."
  else
    render :edit
  end
end

特にredirect_to profile_path

ログを見ると、次のシーケンスが表示されます。

POST "/password_resets/qlslPnuOhdyMCNseMnV3bA"
GET  "/profiles/qlslPnuOhdyMCNseMnV3bA"

ルートはとである必要が/password_resets/:idあり/profiles/:idます。はトークンを必要としますが/password_resets、ユーザーの数値IDを必要とします。'qlslPnuOhdyMCNseMnV3bA'/profiles

コントローラに戻ると、次のように表示されます。

redirect_to profile_path, :notice => "Great news: Your password has been reset."

profile_pathどのユーザーを使用するかがわからないparams[:id]ため、間違った/profiles/qlslPnuOhdyMCNseMnV3bAURLを作成しているようです。profile_path次のような方法で使用するユーザーを指定してみてください。

redirect_to profile_path(@user), :notice => "Great news: Your password has been reset."
于 2012-01-26T23:18:26.637 に答える