我有模型series
seasons
和episodes
。從series/new
的形式我想創建一個系列,季節和情節一起。我正在閱讀Rails Routing和Nested Forms指南,但我不知道我在做什麼錯,因爲指南並未涵蓋3級的深度。當使用嵌套表單時,Rails僅插入Series
和Season
值而不是Episode
值。導軌嵌套屬性3級深度
我的方法更正確嗎?我會很感激任何輸入〜
# 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 %>
你是否在params散列中接收了episod的屬性?如果是的話,那麼你是否列入了插曲的屬性? –
感謝您的回覆,這是我的series_params:'def series_params params.require(:person).permit(:title,seasons_attributes:[:id,:title],episodes_attributes:[:id,:title]) end ' – Dotol