重置密码时,更新功能停止在rails中工作

时间:2017-06-28 00:32:15

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

timeout when uodating

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

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

这是我的编辑文件:

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

以下是日志:enter image description here

这是我的用户表: enter image description here

以下是将password_reset_path(params [:id])更改为password_reset_path(@user)后的日志 enter image description here

1 个答案:

答案 0 :(得分: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]"
  },
  ...
}

有了这个,你的参数现在就是:

# Python program to insert in sorted list

# Node class 
class Node:

    # Constructor to initialize the node object
    def __init__(self, data):
        self.data = data
        self.next = None

class LinkedList:

    # Function to initialize head
    def __init__(self):
        self.head = None

    def sortedInsert(self, new_node):

        # Special case for the empty linked list 
        if self.head is None:
            new_node.next = self.head
            self.head = new_node

        # Special case for head at end
        elif self.head.data <= new_node.data:
            new_node.next = self.head
            self.head = new_node

        else :

            # Locate the node before the point of insertion
            current = self.head
            while(current.next is not None and
                 current.next.data > new_node.data):
                current = current.next

            new_node.next = current.next
            current.next = new_node

    # Function to insert a new node at the beginning
    def push(self, new_data):
        new_node = Node(new_data)
        new_node.next = self.head
        self.head = new_node

    # Utility function to prit the linked LinkedList
    def printList(self):
        temp = self.head
        while(temp):
            print(temp.data),
            temp = temp.next

    def pop(self):
        val = self.head.data
        self.head = self.head.next
        return val


# Driver program
llist = LinkedList()
new_node = Node(5)
llist.sortedInsert(new_node)
new_node = Node(10)
llist.sortedInsert(new_node)
new_node = Node(7)
llist.sortedInsert(new_node)
new_node = Node(3)
llist.sortedInsert(new_node)
new_node = Node(1)
llist.sortedInsert(new_node)
new_node = Node(9)
llist.sortedInsert(new_node)
print("Create Linked List")
llist.printList()