2017-09-05 260 views
1

我有模型seriesseasonsepisodes。從series/new的形式我想創建一個系列,季節和情節一起。我正在閱讀Rails RoutingNested Forms指南,但我不知道我在做什麼錯,因爲指南並未涵蓋3級的深度。當使用嵌套表單時,Rails僅插入SeriesSeason而不是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 %> 
+0

你是否在params散列中接收了episod的屬性?如果是的話,那麼你是否列入了插曲的屬性? –

+0

感謝您的回覆,這是我的series_params:'def series_params params.require(:person).permit(:title,seasons_attributes:[:id,:title],episodes_attributes:[:id,:title]) end ' – Dotol

回答

0

您需要從該系列機型移動accepts_nested_attributes_for :episodes以來季節形式季節模型,<%= seasons_form.fields_for :episodes ... %>是接受屬性爲episodes

+0

這會在我的構建語句中拋出一個未定義的方法'episodes'。 – Dotol

1

更換

# 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 
+0

這很有道理實際上不能相信我想到的是,一旦我有權訪問計算機,檢查一下。 – Dotol

+1

這[代碼](https://github.com/rails/rails/blob/d0d7ce001bdbb0e5551da881caf8156c06238b84/activerecord/lib/active_record/nested_attributes.rb)顯示了rails如何定義'accep_nested_attributes_for'。 如果'如果反射= _reflect_on_association(association_name)'是零, '提高引發ArgumentError, 「沒有協會發現名稱\'#{association_name}」。它是否尚未定義的?」' 發生。 – kansiho

+0

謝謝@Aoi_777我更新了原始帖子,以進一步詳細說明劇集未如何保存 – Dotol

1

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 

accepts_nested_attributes_for :episodes 

episode.rb

belongs_to :season 

正如你已經使用has_many :episodes, :through => :seasons,情節參數將裏面hashseasons。所以,你需要做一個細微的變化在series_params方法:

系列控制器

def new 
    @series = Series.new 
    #build objects of nested 
    @season = @series.seasons.build 
    @episod = @season.episods.build 
end 

def series_params 
    params.require(:series).permit(:title, 
    seasons_attributes: [:id, :title, episodes_attributes: [:id, :title]]) 
end 

注:這是靜態的。對於動態對象構建,這裏是一個很好的screencast

+0

我確實做到了這一點,它仍然沒有保存劇集這些是我的參數和逐字母的形式:def series_params 'params.require :series).permit(:title,seasons_attributes:[:id,:title,episodes_attributes:[:id,:title]]) end' – Dotol

+0

我的表單:'<%= form_for @series do | f | %> <%= f.text_field:title%> <%= f.fields_for:seasons do | ff | %> <%= ff.text_field:title%> <%= ff.fields_for:episodes do | fff | %> <%= fff.text_field:標題%> <% end %> <% end %> <%= f.submit%> <% end %>' – Dotol

+1

你可以發表你的輸入PARAMS哈希?提交表單時,您可以在服務器日誌中找到它。 –