【问题标题】:When resetting password, update function stops working in rails重置密码时,更新功能在rails中停止工作
【发布时间】:2017-12-01 04:40:10
【问题描述】:

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

这是我的编辑文件:

    <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) 后的日志

【问题讨论】:

  • 您的参数似乎为空。问题在于你的形式。你能从你的日志中发布你的参数吗?

标签: ruby-on-rails ruby ruby-on-rails-4 reset-password


【解决方案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 发送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]"
  },
  ...
}

【讨论】:

  • 如果我想保留 params[:user][:password].empty 怎么办?我该怎么办?顺便问一下,你怎么知道表单发送的是成员密钥而不是用户?
  • 我怎样才能让用户的表单键不是会员?
  • 我现在可以知道问题出在哪里。谢谢。但是在我变成会员之后。还有一个问题。 NoMethodError(#<0x007f9642b8d140>
猜你喜欢
  • 2013-12-04
  • 2018-03-17
  • 1970-01-01
  • 1970-01-01
  • 2014-04-21
  • 1970-01-01
  • 2018-02-18
  • 2017-10-29
  • 1970-01-01
相关资源
最近更新 更多