العبارة غير صالحة عند محاولة تنفيذ كلمة المرور_reset Railscast

StackOverflow https://stackoverflow.com/questions/9014474

سؤال

أحاول التنفيذ ريلسكاست #274 في تطبيقي حتى أتمكن من توفير خيار إعادة تعيين كلمة المرور.لقد وصلت إلى النقطة التي يمكنني فيها كتابة بريدي الإلكتروني في نموذج لإعادة تعيين كلمة المرور وتلقي بريد إلكتروني يحتوي على رابط لإعادة تعيين كلمة المرور.بدأت الأمور تسوء عندما أدخل كلمة المرور الجديدة وأحاول حفظها.انتهى بي الأمر مع Action Controller:Exception caught.إليك ما أظهره السجل الخاص بي بعد أن أرسلت لنفسي بريدًا إلكترونيًا يحتوي على رابط إعادة تعيين كلمة المرور:

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

النقر على رابط إعادة تعيين كلمة المرور:

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 قبل إعادة زرع قاعدة البيانات.هل يمكن أن يكون هذا بسبب عدم تشغيل برنامج نصي لمنح المستخدمين الحاليين كلمة مرور_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
هل كانت مفيدة؟

المحلول

يبدو أن المشكلة في جهازك 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 يريد 'qlslPnuOhdyMCNseMnV3bA' رمز ولكن /profiles يريد المعرف الرقمي للمستخدم.

وبالعودة إلى وحدة التحكم نرى هذا:

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

أنت لا تخبر profile_path المستخدم الذي يجب استخدامه لذلك يبدو أنه يستحوذ على اهتمامه params[:id] لبناء غير صحيح /profiles/qlslPnuOhdyMCNseMnV3bA عنوان URL.حاول أن تقول profile_path المستخدم الذي يجب استخدامه مع شيء مثل هذا:

redirect_to profile_path(@user), :notice => "Great news: Your password has been reset."
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top