gpt4 book ai didi

ruby-on-rails - Rails 嵌套属性 3 级深度

转载 作者:行者123 更新时间:2023-12-04 02:03:02 24 4
gpt4 key购买 nike

我有模型 series seasonsepisodes。我想从 series/new 表单中创建一个系列、一季和一集。我正在阅读 Rails RoutingNested Forms指南,但我不知道我做错了什么,因为指南没有涵盖 3 级深度。使用嵌套表单时,Rails 只会插入 SeriesSeason但不会插入 Episode

我的方法是否正确?我会很感激任何输入~

# series.rb
has_many :seasons, dependent: :destroy
has_many :episodes, :through => :seasons
accepts_nested_attributes_for :seasons
accepts_nested_attributes_for :episodes

# season.rb
belongs_to :series
has_many :episodes, dependent: :destroy

# episode.rb
belongs_to :season

# routes.rb
resources :series, except: [:show, :new] do
resources:seasons, except: [:show], path: '' do
resources :episodes, path: ''
end
end

系列/new.html.erb

<%= form_for @series do |f| %>
<%= f.text_field :title %>
<%= f.fields_for :seasons do |seasons_form| %>
<%= seasons_form.text_field :title %>
<%= seasons_form.fields_for :episodes do |episodes_form| %>
<%= episodes_form.text_field :title %>
<% end %>
<% end %>
<% end %>

最佳答案

替换

# series.rb
has_many :seasons, dependent: :destroy
accepts_nested_attributes_for :seasons
accepts_nested_attributes_for :episodes

# series.rb
has_many :seasons, dependent: :destroy
has_many :episodes, :through => :seasons
accepts_nested_attributes_for :seasons
accepts_nested_attributes_for :episodes

关于ruby-on-rails - Rails 嵌套属性 3 级深度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46046187/

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