Rails 6 字段必填但不接受 0 或 Null
Rails 6 Field Required but will not accept 0 or Null
我已将我的项目从 Rails 4 转移到 Rails 6,直到最近,一切正常,但现在当我更新数据库中的特定条目时,出现错误说明“老板必须存在”。问题是有一群人是老板却没有老板。
我已经检查了我的架构,该字段不是必需的。 (boss_id)
person.rb
class Person < ApplicationRecord
belongs_to :boss, class_name: 'Person'
has_many :subordinates, class_name: 'Person', foreign_key: 'boss_id'
end
form.html.erb
<fieldset>
<label>
Boss
<%= f.select :boss_id, [[" ", :null], ["Name1", 1], ["Name2", 2], ... etc ... %>
</label>
</fieldset>
adminnamespace/people_controller.rb
class NameSpace::PeopleController < NameSpaceController
def index
@people = Person.all
end
def show
@person = Person.find(params[:id])
end
def new
@person = Person.new
end
def create
@person = Person.new(person_params)
if @person.save
flash[:notice] = "Person created successfully!"
redirect_to namespace_person_path(@person)
else
render :new, status: :unprocessable_entity
end
end
def edit
@person = Person.find(params[:id])
end
def update
@person = Person.find(params[:id])
if @person.update(person_params)
redirect_to namespace_person_path(@person)
else
render :edit, status: :unprocessable_entity
end
end
def destroy
person = Person.find(params[:id])
person.destroy
redirect_to namespace_people_path
end
private
def person_params
params.require(:person).permit(
:uname, ... :boss_id)
end
end
如果 boss_id 设置为 0 或没有对应记录的数字,我会收到此错误。我需要变通办法还是有办法让它像以前那样工作?
您应该能够将 required: false
添加到 belongs_to
语句。
例如:
class Person < ApplicationRecord
belongs_to :boss, class_name: 'Person', required: false
has_many :subordinates, class_name: 'Person', foreign_key: 'boss_id'
end
我已将我的项目从 Rails 4 转移到 Rails 6,直到最近,一切正常,但现在当我更新数据库中的特定条目时,出现错误说明“老板必须存在”。问题是有一群人是老板却没有老板。
我已经检查了我的架构,该字段不是必需的。 (boss_id)
person.rb
class Person < ApplicationRecord
belongs_to :boss, class_name: 'Person'
has_many :subordinates, class_name: 'Person', foreign_key: 'boss_id'
end
form.html.erb
<fieldset>
<label>
Boss
<%= f.select :boss_id, [[" ", :null], ["Name1", 1], ["Name2", 2], ... etc ... %>
</label>
</fieldset>
adminnamespace/people_controller.rb
class NameSpace::PeopleController < NameSpaceController
def index
@people = Person.all
end
def show
@person = Person.find(params[:id])
end
def new
@person = Person.new
end
def create
@person = Person.new(person_params)
if @person.save
flash[:notice] = "Person created successfully!"
redirect_to namespace_person_path(@person)
else
render :new, status: :unprocessable_entity
end
end
def edit
@person = Person.find(params[:id])
end
def update
@person = Person.find(params[:id])
if @person.update(person_params)
redirect_to namespace_person_path(@person)
else
render :edit, status: :unprocessable_entity
end
end
def destroy
person = Person.find(params[:id])
person.destroy
redirect_to namespace_people_path
end
private
def person_params
params.require(:person).permit(
:uname, ... :boss_id)
end
end
如果 boss_id 设置为 0 或没有对应记录的数字,我会收到此错误。我需要变通办法还是有办法让它像以前那样工作?
您应该能够将 required: false
添加到 belongs_to
语句。
例如:
class Person < ApplicationRecord
belongs_to :boss, class_name: 'Person', required: false
has_many :subordinates, class_name: 'Person', foreign_key: 'boss_id'
end