gpt4 book ai didi

ruby-on-rails - 返回 : Collection select with with include_blank won't allow nil

转载 作者:太空宇宙 更新时间:2023-11-03 17:45:36 25 4
gpt4 key购买 nike

我的表单中有一个集合选择:

<div class="field">
<%= f.label :area %>
<%= f.collection_select(:area_id, Area.all, :id, :name, include_blank: "No area.") %>

而且我的模型验证对面积没有要求。

据我了解,通过使用 include_blank 可以让我选择 nil。但是我收到验证错误“区域必须存在”

编辑:

这是模型中的重要代码:

has_many :ratings, dependent: :destroy
has_many :noise_ratings, dependent: :destroy
has_many :statuses, dependent: :destroy
has_many :checkins, dependent: :destroy

has_and_belongs_to_many :features

belongs_to :area
belongs_to :campus

validates :name, presence: true, uniqueness: { scope: :campus_id, message: "unique space for each campus." }
validates :description, presence: true
validates :campus_id, presence: true

最佳答案

Rails 5 强制您设置所有 belongs_to 关联,除非您指定 optional: true。添加它是为了防止数据不一致,因此,如果您希望它的行为像以前的 Rails 版本一样,您只需将关联更改为:

belongs_to :area, optional: true

关于ruby-on-rails - 返回 : Collection select with with include_blank won't allow nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38535383/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com