未定义的方法`with_indifferent_access'为零:NilClass

时间:2017-11-02 07:07:06

标签: ruby-on-rails web-services devise ruby-on-rails-5

我试图用设计覆盖重置密码控制器,但无论如何它给了我这个错误。我无法理解究竟是什么原因。我正在分享我的代码

passwords_controller.rb

class Api::V1::PasswordsController < Devise::PasswordsController

    skip_before_action :require_login!


    def new
        @user = User.new
    end

    def create
      @user = User.send_reset_password_instructions(params[:user])
      if successfully_sent?(@user)
        head :status => 200
      else
        render :status => 422, :json => { :errors => @user.errors.full_messages }
      end
    end

end

路由

 devise_for :users,controllers: { 
    sessions: 'api/v1/sessions',
    registrations: 'api/v1/registrations',
    :passwords => 'api/v1/passwords'

  }

邮差

我正在使用参数

来访问此网址
http://localhost:3000/users/password
Method - Post
Body (raw)

    {
        "email": "some-email@gmail.com"
    }

获取此错误 NoMethodError(未定义的方法`with_indifferent_access&#39;对于nil:NilClass):

2 个答案:

答案 0 :(得分:1)

我在模型上调用了send_reset_password_instructions。应该在用户实例上调用它。这是代码

def create
  @user = User.find_by(email: params[:email])
  @user.send_reset_password_instructions
  if successfully_sent?(@user)
    head :status => 200
  else
    render :status => 422, :json => { :errors => @user.errors.full_messages }
  end
end

答案 1 :(得分:1)

必须在用户实例而不是模型上调用send_reset_password_instructions。