3

I have the models series seasons and episodes. From within series/new form I want to create a series, season and episode all together. I'm reading Rails Routing and Nested Forms guides but I don't know what I'm doing wrong as the guide doesn't cover a 3 level depth. When using the nested form, Rails is only inserting the Series and Season values but not the Episode values.

Is my approach even correct? I'd appreciate any input~

# 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

series/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 %>
5
  • Are you receiving episod's attributes in params hash? If yes, then have you whitelisted episode's attributes? Commented Sep 5, 2017 at 7:10
  • Thanks for the reply, this is my series_params: def series_params params.require(:person).permit(:title, seasons_attributes: [:id, :title], episodes_attributes: [:id, :title]) end Commented Sep 5, 2017 at 17:36
  • 1
    @Dotol were you able to solve this problem? Commented Aug 15, 2018 at 20:05
  • I wasn't able to, gave up a while ago. I'd appreciate some insight into what I was doing wrong or a possible solution/workaround Commented Aug 16, 2018 at 22:01
  • I know this is 5 years old but I currently have the same problem. Any solutions? Commented Jun 2, 2022 at 16:43

3 Answers 3

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

accepts_nested_attributes_for :episodes

episode.rb

belongs_to :season

As you have used has_many :episodes, :through => :seasons , episodes parameters will be inside the hash of seasons. So you need to make a slight change in series_params method:

series controller

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

Note: It is static. For dynamic object building, here is a good screencast

Sign up to request clarification or add additional context in comments.

11 Comments

I did precisely that and it still doesn't save episodes these are my params and form letter by letter: def series_params params.require(:series).permit(:title, seasons_attributes: [:id, :title, episodes_attributes: [:id, :title]]) end
can you post your input params hash? You can find it on server log when you submit the form.
In your season model, accept its nested params, that is episode's attributes. I have updated my answer.
It is because, you may not have build episod's object in your controller's new method.
For time being, can you remove nested routes and make them individual like resources :series, resources :seasons, resources :episods
|
2

Replace

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

with

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

3 Comments

This makes sense actually can't believe I thought of that, checking in a bit once I have access to a computer.
This code shows how rails defined accepts_nested_attributes_for. if if reflection = _reflect_on_association(association_name) is nil, raise ArgumentError, "No association found for name `#{association_name}'. Has it been defined yet?" occurred.
Thanks @Aoi_777 I updated the original post to further detail how episodes isn't being saved
0

You need to move accepts_nested_attributes_for :episodes from the series model to the season model since the seasons form, <%= seasons_form.fields_for :episodes ... %> is accepting attributes for the episodes

1 Comment

That would throw an undefined method 'episodes' in my build statement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.