0

更新时超时

这是我的代码: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

情况是在我输入密码重置令牌的路径后,我输入了新密码和密码确认。然后我点击更新。但是,此错误页面显示。我真的不知道如何解决这个问题。

这是我的编辑文件:

    <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>

这是日志:在此处输入图像描述

这是我的用户表: 在此处输入图像描述

这是将 password_reset_path(params[:id]) 更改为 password_reset_path(@user) 后的日志 在此处输入图像描述

4

1 回答 1

1

问题是因为您的form发送member密钥而不是user,如您的日志所示:

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

因此,由于没有user键,因此params[:user]返回nil,并且由于它无法响应[],因此导致错误:

nil:NilClass 的未定义方法“[]”

查看您的控制器并查看我看不出为什么member要使用而不是user,但是您可以强制您使用form发送user密钥as: "user",如下所示:

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

有了这个,您的参数现在将是:

Parameters: { 
  "utf8"=>"✓", 
  "authenticity_token"=>"...",
  "email"=>"naomi.wuuu@gamil.com",
  "user"=>{
    "password"=>"[FILTERED]",
    "password_confirmation"=>"[FILTERED]"
  },
  ...
}
于 2017-06-28T02:23:22.950 回答