问题描述
我有这个错误,当我尝试发布用户时发生,我无法理解,请帮助我我不知道还能做什么,在做这篇文章之前我没有找到任何东西,tks。 我的文件:
registrations_controller.rb
class RegistrationsController < ApplicationController
respond_to :json
def create
build_resource(sign_up_params)
resource.save
if resource.errors.empty?
render json: resource
else
render json: resource.errors
end
end
end
sessions_controller.rb
class SessionsController < ApplicationController
respond_to :json
private
def respond_with(resource,_opts = {})
render json: resource
end
def respond_to_on_destroy
head :no_content
end
end
users_controller.rb
class UsersController < ApplicationController
before_action :set_user,only: [:show,:update,:destroy]
# GET /users
def index
@users = User.all
render json: @users
end
# GET /users/1
def show
render json: @user
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
# PATCH/PUT /users/1
def update
if @user.update(user_params)
render json: @user
else
render json: @user.errors,status: :unprocessable_entity
end
end
# DELETE /users/1
def destroy
@user.destroy
end
private
# Use callbacks to share common setup or constraints between actions.
def set_user
@user = User.find(params[:id])
end
# Only allow a list of trusted parameters through.
def user_params
params.require(:user).permit(:email,:password)
end
end
model/user.rb
class User < ApplicationRecord
include Devise::JWT::RevocationStrategies::JTIMatcher
devise :registerable,:database_authenticatable,:jwt_authenticatable,jwt_revocation_strategy: self
end
routes.rb
Rails.application.routes.draw do
devise_for :users,path: '',path_names: {
sign_in: 'login',sign_out: 'logout',registration: 'signup'
},controllers: {
sessions: 'sessions',registrations: 'registrations'
}
end
migrate/create_user.rb
class createusers < ActiveRecord::Migration[6.1]
def change
create_table :users do |t|
t.string :email
t.string :password
t.string :jti,null: false,default: ""
t.timestamps
end
end
end
迁移/add_devise_to_users.rb
class AddDevisetoUsers < ActiveRecord::Migration[6.1]
def self.up
change_table :users do |t|
## Database authenticatable
# t.string :email,default: ""
t.string :encrypted_password,default: ""
## Recoverable
# t.string :reset_password_token
# t.datetime :reset_password_sent_at
## Rememberable
# t.datetime :remember_created_at
## Trackable
# t.integer :sign_in_count,default: 0,null: false
# t.datetime :current_sign_in_at
# t.datetime :last_sign_in_at
# t.string :current_sign_in_ip
# t.string :last_sign_in_ip
## Confirmable
# t.string :confirmation_token
# t.datetime :confirmed_at
# t.datetime :confirmation_sent_at
# t.string :unconfirmed_email # Only if using reconfirmable
## Lockable
# t.integer :Failed_attempts,null: false # Only if lock strategy is :Failed_attempts
# t.string :unlock_token # Only if unlock strategy is :email or :both
# t.datetime :locked_at
# t.timestamps null: false
end
add_index :users,:email,unique: true
# add_index :users,:reset_password_token,:confirmation_token,:unlock_token,unique: true
end
def self.down
raise ActiveRecord::IrreversibleMigration
end
end
schema.rb
ActiveRecord::Schema.define(version: 2021_03_19_040209) do
create_table "users",force: :cascade do |t|
t.string "email"
t.string "password"
t.string "jti",default: "",null: false
t.datetime "created_at",precision: 6,null: false
t.datetime "updated_at",null: false
t.string "encrypted_password",null: false
t.index ["email"],name: "index_users_on_email",unique: true
end
end
Gemfile
source 'https://rubygems.org'
ruby '2.7.2'
gem 'rails','~> 6.1.3'
gem 'sqlite3','~> 1.4'
gem 'puma','~> 5.0'
gem 'bootsnap','>= 1.4.4',require: false
group :development,:test do
gem 'devise'
gem 'devise-jwt'
gem 'byebug',platforms: [:mri,:mingw,:x64_mingw]
end
group :development do
gem 'listen','~> 3.3'
gem 'spring'
end
gem 'tzinfo-data',platforms: [:mingw,:mswin,:x64_mingw,:jruby]
解决方法
RegistrationsController
应该继承自 Devise::RegistrationsController
,因为您试图覆盖它。完成此操作后,您就可以访问 sign_up_params
实例方法。
class RegistrationsController < Devise::RegistrationsController
# ...
end