[英]Belongs_to relationship in edit
I'm encountering a problem when editing a form with a belongs_to relationship (extra_guest belongs_to age_table).我在编辑具有belongs_to 关系(extra_guest belongs_to age_table)的表单时遇到问题。
I am able to create a new extra_guest and assign it to an age_table, but I cannot get the edit/update to work as my update function returns a falseClass.--> @extra_guest.update(extra_guest_params).errors.full_messages
returns undefined method `errors' for false:FalseClass
我能够创建一个新的 extra_guest 并将其分配给一个 age_table,但是我无法让编辑/更新工作,因为我的更新 function 返回一个 falseClass。-->
@extra_guest.update(extra_guest_params).errors.full_messages
返回undefined method `errors' for false:FalseClass
Code代码
models楷模
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 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
views/extra_guests/form意见/额外的客人/表格
<%= 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 %>
Error message + params错误信息 + 参数
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"}
I think you show anticipate the errors:我认为你表现出预期的错误:
if @extra_guest.update(extra_guest_params)
.. do something for success..
else
do something for error
flash[:notice] = @extra_guest.errors.full_messages
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.