尝试在 rails 中发出 DELETE 请求时出现 500 内部服务器错误并查看错误的 table
500 Internal Server Error and looking through the wrong table when trying to make a DELETE request in rails
我试图让一些基本的 CRUD 功能在我的 rails 应用程序中工作,但特别是 DELETE 一直显示 500 内部服务器错误。
Started DELETE "/developers/7" for ::1 at 2021-09-27 13:40:00 +0530
Processing by DevelopersController#destroy as */*
Parameters: {"id"=>"7"}
(0.2ms) SELECT sqlite_version(*)
↳ app/controllers/developers_controller.rb:44:in `set_developer'
Developer Load (0.4ms) SELECT "developers".* FROM "developers" WHERE "developers"."id" = ? LIMIT ? [["id", 7], ["LIMIT", 1]]
↳ app/controllers/developers_controller.rb:44:in `set_developer'
TRANSACTION (0.1ms) begin transaction
↳ app/controllers/developers_controller.rb:38:in `destroy'
TRANSACTION (0.1ms) rollback transaction
↳ app/controllers/developers_controller.rb:38:in `destroy'
Completed 500 Internal Server Error in 9ms (ActiveRecord: 1.6ms | Allocations: 2233)
ActiveRecord::StatementInvalid (Could not find table 'developers_teams'):
app/controllers/developers_controller.rb:38:in `destroy'
我想让它查看的实际模型是 developer
并且 get 和 post 请求做得很好,但它一直说找不到 table developers_teams
这是我加入 table 的 developer
和 team
之间的 HABTM 关联。
相关机型:
team.rb
class Team < ApplicationRecord
validates :name, presence: true
has_many :messages
has_and_belongs_to_many :developers
end
developer.rb
class Developer < ApplicationRecord
validates :full_name, presence: true
has_and_belongs_to_many :teams
end
开发者控制器:
class DevelopersController < ApplicationController
before_action :set_developer, only: [:show, :update, :destroy]
# GET /developers
def index
@developers = Developer.all
render json: @developers
end
# GET /developers/1
def show
render json: @developer
end
# POST /developers
def create
@developer = Developer.new(developer_params)
if @developer.save
render json: @developer, status: :created, location: @developer
else
render json: @developer.errors, status: :unprocessable_entity
end
end
# PATCH/PUT /developers/1
def update
if @developer.update(developer_params)
render json: @developer
else
render json: @developer.errors, status: :unprocessable_entity
end
end
# DELETE /developers/1
def destroy
@developer.destroy
end
private
# Use callbacks to share common setup or constraints between actions.
def set_developer
@developer = Developer.find(params[:id])
end
# Only allow a list of trusted parameters through.
def developer_params
params.require(:developer).permit(:full_name, :email, :mobile)
end
end
加入table架构:
class AddHabtmForTeamsAndDevelopers < ActiveRecord::Migration[6.1]
def change
create_table :teams_developers, id: false do |t|
t.belongs_to :team
t.belongs_to :developer
end
end
end
默认情况下,has_and_belongs_to_many
需要一个 table 名称与按字母顺序 连接的模型名称 ,因此它应该是 developers_teams
。
您可以通过设置 join_table
选项来覆盖默认约定:
has_and_belongs_to_many :teams, join_table: 'teams_developers'
现在,它查看 developers_team
table 的原因是 rails 想要删除引用记录的连接 table 中的所有连接记录正在删除。
我试图让一些基本的 CRUD 功能在我的 rails 应用程序中工作,但特别是 DELETE 一直显示 500 内部服务器错误。
Started DELETE "/developers/7" for ::1 at 2021-09-27 13:40:00 +0530
Processing by DevelopersController#destroy as */*
Parameters: {"id"=>"7"}
(0.2ms) SELECT sqlite_version(*)
↳ app/controllers/developers_controller.rb:44:in `set_developer'
Developer Load (0.4ms) SELECT "developers".* FROM "developers" WHERE "developers"."id" = ? LIMIT ? [["id", 7], ["LIMIT", 1]]
↳ app/controllers/developers_controller.rb:44:in `set_developer'
TRANSACTION (0.1ms) begin transaction
↳ app/controllers/developers_controller.rb:38:in `destroy'
TRANSACTION (0.1ms) rollback transaction
↳ app/controllers/developers_controller.rb:38:in `destroy'
Completed 500 Internal Server Error in 9ms (ActiveRecord: 1.6ms | Allocations: 2233)
ActiveRecord::StatementInvalid (Could not find table 'developers_teams'):
app/controllers/developers_controller.rb:38:in `destroy'
我想让它查看的实际模型是 developer
并且 get 和 post 请求做得很好,但它一直说找不到 table developers_teams
这是我加入 table 的 developer
和 team
之间的 HABTM 关联。
相关机型:
team.rb
class Team < ApplicationRecord
validates :name, presence: true
has_many :messages
has_and_belongs_to_many :developers
end
developer.rb
class Developer < ApplicationRecord
validates :full_name, presence: true
has_and_belongs_to_many :teams
end
开发者控制器:
class DevelopersController < ApplicationController
before_action :set_developer, only: [:show, :update, :destroy]
# GET /developers
def index
@developers = Developer.all
render json: @developers
end
# GET /developers/1
def show
render json: @developer
end
# POST /developers
def create
@developer = Developer.new(developer_params)
if @developer.save
render json: @developer, status: :created, location: @developer
else
render json: @developer.errors, status: :unprocessable_entity
end
end
# PATCH/PUT /developers/1
def update
if @developer.update(developer_params)
render json: @developer
else
render json: @developer.errors, status: :unprocessable_entity
end
end
# DELETE /developers/1
def destroy
@developer.destroy
end
private
# Use callbacks to share common setup or constraints between actions.
def set_developer
@developer = Developer.find(params[:id])
end
# Only allow a list of trusted parameters through.
def developer_params
params.require(:developer).permit(:full_name, :email, :mobile)
end
end
加入table架构:
class AddHabtmForTeamsAndDevelopers < ActiveRecord::Migration[6.1]
def change
create_table :teams_developers, id: false do |t|
t.belongs_to :team
t.belongs_to :developer
end
end
end
默认情况下,has_and_belongs_to_many
需要一个 table 名称与按字母顺序 连接的模型名称 ,因此它应该是 developers_teams
。
您可以通过设置 join_table
选项来覆盖默认约定:
has_and_belongs_to_many :teams, join_table: 'teams_developers'
现在,它查看 developers_team
table 的原因是 rails 想要删除引用记录的连接 table 中的所有连接记录正在删除。