1

I started writing an app which reviews books, articles etc(model name: Piece ) and has the option to review chapters or smaller sections (Sections, Subsections, and Subsubsections models)

I want to be able to generate a general overview of the Piece where all of the nested Sections, Subsections, and Subsubsections and their attributes will be listed in one view. So my routes.rb file looks like this:

  resources :pieces do
     resources :sections do
       resources :subsections do 
         resources :subsubsections
       end
     end
   end

Which I have since learned is sloppy but its too late for me now. For now, I have been able to do this from the subsubsections, because it has acces to all the parent parameters (piece_id, section_id, etc). But this means that I have to jump down the tree into the Subsubsection view and then go from there, whereas I would like to be able to access all these parameters from the Piece controller so that I can link straight from the Piece view to the more general overview, without having to go all the way down the chain.

I apologize if this is a simple question, I only started recently.

My models like like this:

class Subsubsection < ActiveRecord::Base
  belongs_to :subsection
end

class Subsection < ActiveRecord::Base
  belongs_to :section
  has_many :subsubsections
end

class Section < ActiveRecord::Base
    belongs_to :piece
    has_many :subsections
end

class Piece < ActiveRecord::Base
    has_many :sections
    has_many :links
end

And the way i am accessing the general overview for now is using the index action of the subsubsections controller:

def index
    @piece = Piece.find(params[:piece_id])
    @section = @piece.sections.find(params[:section_id])
    @subsection = @section.subsections.find(params[:subsection_id])
end

My view accesses it like this:

<p><%= link_to "'General Piece Overview", piece_section_subsection_subsubsections_path(@piece, @section, @subsection), class: 'section_name' %></p>

Heres the link to the project in case it helps:

https://github.com/kingdavidek/StuddyBuddy

1 Answers1

0

Your question has some internal tension; you say you want access to the nested parameters from your PieceController, but if the request has been routed to PieceController, those nested parameters will not be present. When nested parameters are present, the request will be routed to the most specific controller class for which the request provides a parameter.

If the question can be rephrased as "how, from within PieceController can I generate something like a table of contents?" the answer looks something like this.

In PieceController:

def show
  @piece = Piece.find(params[:id]).include(sections: {subsections: :subsubsections}})
end

In app/views/pieces/show.html.erb

<% @piece.sections.each do |section| %>
  <%= link_to piece_section_path(@piece, section), section.title %>
  <% section.subsections.each do |subsection| %>
    <%= link_to piece_section_subsection_path(@piece, section, subsection), subsection.title %>
    <% subsection.subsubsections.each do |subsubsection| %>
      <%= link_to piece_section_subsection_subsubsection_path(@piece, section, subsection, subsubsection), subsubsection.title %>
      <% end %>
    <% end %>
  <% end %>
<% end %>

This makes some assumptions, but hopefully the idea is clear.

Jim Van Fleet
  • 1,110
  • 7
  • 12
  • Hi, Thanks for responding! As I understand, it is not possible to do what I was doing originally, which is fine. I tried to implement what you where doing by using the includes method but i get a **"NoMethodError in PiecesController#show", undefined method `includes' for #.** Do you have any idea why this is? – Kingdavidek Aug 17 '16 at 09:27