#<nomethoderror:undefined =“” method =“”`token?'=“” for =“”#<user:=“”>

时间:2018-12-23 13:47:44

标签: ruby-on-rails-5

当我尝试创建用户时,我遇到此错误

"exception": "#<NoMethodError: undefined method `token?' for #<User:0x000056037eec27a0>>",

用户的数据库是

 class CreateUsers < ActiveRecord::Migration[5.2]
  def change
    create_table :users do |t|
      t.string :name
      t.string :email
      t.string :phone
      t.string :token
      t.string :password_digest
      t.timestamps
    end
    add_index :users, unique: true
  end
end

我的用户模型代码是

class User < ApplicationRecord
has_secure_token
has_secure_password
has_many :projects , dependent: :destroy
end

我的控制器代码是

class UsersController < ApplicationController
  def new
    @user = User.new
  end

  # POST /users
  def create
    @user = User.new(user_params)

    if @user.save
      render json: @user, status: :created, location: @user
    else
      render json: @user.errors, status: :unprocessable_entity
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_user
      @user = User.find(params[:id])
    end

    # Only allow a trusted parameter "white list" through.
    def user_params
      Rails.logger.info "#{params.inspect}"
      params.require(:user).permit(:name, :email, :phone, :password)
    end
end

我通过POST请求发送的数据是

{  
"user":{
      "name":"pranesh",
      "email":"pranesh@gmail.com",
      "phone":"987654321",
      "password":"helloworld"
      }
}

我发送请求时遇到此错误,

 "status": 500,
    "error": "Internal Server Error",
    "exception": "#<NoMethodError: undefined method `token?' for #<User:0x000056037eec27a0>>",

我正在使用滑轨5.2.1

1 个答案:

答案 0 :(得分:0)

has_secure_token :token

尝试一下