ruby-on-rails - 编辑中的 Belongs_to 关系
问题描述
我在编辑具有belongs_to 关系(extra_guest belongs_to age_table)的表单时遇到问题。
我能够创建一个新的 extra_guest 并将其分配给一个 age_table,但我无法让编辑/更新工作,因为我的更新函数返回一个 falseClass。-->@extra_guest.update(extra_guest_params).errors.full_messages
返回undefined method `errors' for false:FalseClass
代码
楷模
class ExtraGuest < ApplicationRecord
belongs_to :age_table
validates :age_table, presence: true
end
class AgeTable < ApplicationRecord
belongs_to :park
has_many :extra_guests, dependent: :destroy
validates :name, :age_from, :age_to, presence: true
validates_associated :extra_guests
end
class Attraction < ApplicationRecord
belongs_to :park
has_many :extra_guests, dependent: :destroy
accepts_nested_attributes_for :extra_guests, allow_destroy: true
validates :name, presence: true
end
class Park < ApplicationRecord
has_many :attractions, dependent: :destroy
has_many :age_tables, dependent: :destroy
validates :name, :currency, presence: true
end
extra_guests_controller
def edit
@extra_guest = ExtraGuest.find(params[:id])
@age_table = @extra_guest.age_table
@age_table_list = AgeTable.where(park: @attraction.park)
end
def update
@extra_guest = @attraction.extra_guests.find(params[:id])
@age_table = AgeTable.find(params[:age_table])
authorize @extra_guest
if @extra_guest = @extra_guest.update(extra_guest_params)
redirect_to root_path
else
@attraction = Attraction.find(params[:attraction_id])
@extra_guest = ExtraGuest.find(params[:id])
@age_table_list = @attraction.park.age_tables
render 'edit'
end
end
private
def extra_guest_params
params.require(:extra_guest).permit(:name, :age_table_id,
extra_guest_prices_attributes: [:id, :name, :price_type, :start_date, :end_date, :price, :duration, :duration_min, :duration_max, :backend_only, :weekend_extra, :_destroy])
end
意见/额外的客人/表格
<%= simple_form_for [@attraction, @extra_guest] do |f|%>
<%= f.input :age_table, :as => :select, :selected => @age_table.id, :collection => @age_table_list.map {|u| [u.name, u.id]}, :include_blank => false %>
<% f.button :submit %>
错误信息 + 参数
Couldn't find AgeTable without an ID
{"utf8"=>"✓",
"_method"=>"patch",
"authenticity_token"=>"l8HMnVIRybZg==",
"extra_guest"=>
{"age_table"=>"104",
"extra_guest_prices_attributes"=>
{"0"=>{"price"=>"1.0", "weekend_extra"=>"", "start_date"=>"2019-10-15", "end_date"=>"20-09-2019", "duration"=>"", "duration_min"=>"", "duration_max"=>"", "_destroy"=>"false", "id"=>"42"},
"1"=>{"price"=>"1.0", "weekend_extra"=>"", "start_date"=>"2019-10-15", "end_date"=>"2019-10-16", "duration"=>"", "duration_min"=>"", "duration_max"=>"", "_destroy"=>"false", "id"=>"43"}}},
"commit"=>"Save new option",
"attraction_id"=>"185",
"id"=>"55"}
解决方案
方法update
返回true
或false
。你可以在这里找到文档。要检查错误,您需要errors
在@extra_guest
对象上调用方法。
@extra_guest.update(extra_guest_params)
@extra_guest.errors.full_messages
推荐阅读
- google-analytics - 数据工作室通过电子邮件发送的报告页面顺序不正确
- typescript - 如何防止 Typescript 接受具有相同签名的不同函数
- javascript - 如何获取特定名称项目的特定类的总和
- java - 获取作为字符串一部分的数字,然后对字符串进行排序
- unity3d - 将相机输出渲染到渲染纹理并以清晰的背景显示
- recaptcha - Recaptcha 企业隐形 v1 总是返回 0.9 的静态分数
- python - 蒸汽卫士文件
- linux - KDE Plasma 中巨大且不成比例的菜单和图标
- javascript - 如何在同一个应用程序上运行 2 个 ReactI18Next 实例?
- amazon-web-services - AWS CLI - 未找到 AutoScalingGroup 名称 - null