Search code examples
ruby-on-railsrubyruby-on-rails-4reset-password

When resetting password, update function stops working in rails


timeout when uodating

Here is my code: password_resets_controller.rb

        class PasswordResetsController < ApplicationController
      before_action :get_user,         only: [:edit, :update]
      before_action :valid_user,       only: [:edit, :update]
      before_action :check_expiration, only: [:edit, :update]

      def new
      end

      def create
        # @user = User.find_by(email: params[:password_resets]        [:email].downcase)
        @user = User.find_by(email: params[:password_reset][:email].downcase)
        if @user
          @user.create_reset_digest
          @user.send_password_reset_email
          flash[:info] = "Email sent with password reset instructions"
          redirect_to root_url
        else
          flash.now[:danger] = "Email address not found"
          render 'new'
        end
      end

      def edit
      end

      def update
        if params[:member][:password].empty?
          @user.errors.add(:password, "Can't be empty")
          render 'edit'
        elsif @user.update_attributes(user_params)
          # log_in @user
          flash[:success] = "Password has been reset."
          redirect_to @user
        else
          render 'edit'
        end
      end

      private
        def user_params
          params.require(:member).permit(:password, :password_confirmation)
        end

        def get_user
          @user = User.find_by(email: params[:email])
        end

        # Confirms a valid user.
        def valid_user
          unless @user
            redirect_to root_url
          end
        end

        # Check expiration of reset token.
        def check_expiration
          if @user.password_reset_expired?
            flash[:danger] = "Password reset has expired."
            redirect_to new_password_reset_url
          end
        end

    end

Thr situation is after I enter the path of password reset token, I put the new password and password_confirmation. Then I click update. However, this error page shows. I really don't know how to figure this out.

Here is my edit file:

    <div class="row">
      <div class="col-md-6 col-md-offset-3">
        <%= form_for(@user, url: password_reset_path(params[:id])) do |f| %>
          <%= render 'shared/error_messages' %>

          <%= hidden_field_tag :email, @user.email %>

          <%= f.label :password %>
          <%= f.password_field :password, class: 'form-control' %>

          <%= f.label :password_confirmation, "Confirmation" %>
          <%= f.password_field :password_confirmation, class: 'form-control' %>

          <%= f.submit "Update password", class: "btn btn-primary" %>
        <% end %>
      </div>

Here is the log:enter image description here

Here is my User table: enter image description here

Here is the log after change password_reset_path(params[:id]) to password_reset_path(@user) enter image description here


Solution

  • The problem is because your form sends member key instead of user, as your logs show:

    Parameters: { 
      "utf8"=>"✓", 
      "authenticity_token"=>"...",
      "email"=>"naomi.wuuu@gamil.com",
      "member"=>{
        "password"=>"[FILTERED]",
        "password_confirmation"=>"[FILTERED]"
      },
      ...
    }
    

    So, since there is no user key, params[:user] returns nil and, since it can't respond to [], results in the error:

    undefined method `[]' for nil:NilClass

    Looking at your controller and view I can't see why member is being used instead of user, but you can force your form to send user key using as: "user", like this:

    <%= form_for(@user, url: password_reset_path(params[:id]), as: "user") do |f| %>
    

    With this in place, your parameters will be now:

    Parameters: { 
      "utf8"=>"✓", 
      "authenticity_token"=>"...",
      "email"=>"naomi.wuuu@gamil.com",
      "user"=>{
        "password"=>"[FILTERED]",
        "password_confirmation"=>"[FILTERED]"
      },
      ...
    }