【问题标题】:undefined method `with_indifferent_access' for nil:NilClassnil:NilClass 的未定义方法“with_indifferent_access”
【发布时间】:2017-11-02 07:07:05
【问题描述】:

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

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'

  }

邮递员

我用参数点击这个 URL

http://localhost:3000/users/password
Method - Post
Body (raw)

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

收到此错误NoMethodError(nil:NilClass 的未定义方法 `with_indifferent_access'):

【问题讨论】:

  • 你能把:passwords =&gt; "api/v1/passwords"改成passwords: "api/v1/passwords"
  • 它对我不起作用
  • 感谢它现在正在工作

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


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    我在模型上调用 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-27
      • 2013-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多