2

I have a resource Company that has many Projects and People. The index action for the companies displays each company in a table. The show action shows some additional information about it, and then what I'd like to show is another table, with tabs "Projects" and "People".

When I click on the "People" tab, I should go to URL "companies/:id/people", and likewise for the "Projects" tab. I'm not worried about AJAX or pre-loading all of this information into the @company variable during the show action. A simple nested resource is fine.

Now when I'm at "companies/:id/people", it will use PeopleController#index, but I want to show that view (which is JUST it's table, I suppose?) nested within the company's show view. So that when I switch between "companies/:id/people" and "companies/:id/projects", the only thing changing is the table, not the company information around the outside.

Is this sort of thing easily do-able? If Rails isn't build to handle this sort of thing easily, I don't mind using something else. I just don't have much experience with the view layer, so I don't know much about it since I primarily work with JSON.

Logan Serman
  • 29,447
  • 27
  • 102
  • 141

2 Answers2

2

Basic Example:

ProjectsController && PeopleController:

layout :current_layout

def current_layout
  if @company && @company.persisted? && request.path_parameters[:action] == "index" # i prefer helper 'current_action'
    "company"
  else
    "application"
  end
end

Helper:

def parent_layout(layout)
  @view_flow.set(:layout, self.output_buffer)
  self.output_buffer = render(:file => "layouts/#{layout}")
end

Company layout:

#views/layouts/company.html.erb

<h1><%= @company %></h1>
<ul class="tabs">
 <li>Info</li>
 <li>Info</li>
 <li>Info</li>
</ul>
<%= yield %>
<%= parent_layout(:application) %>

People template:

# views/people/index.html.erb

<% if current_layout == "company" %> # just table
  <%= render "people_table" %>
<% else %>
  <h1>People controller</h3>
  <%= render @people %>
<% end %>

Projects template:

# views/projects/index.html.erb

<% if current_layout == "company" %> # just table
  <%= render "projects_table" %>
<% else %>
  <h1>Projects controller</h3>
  <%= render @projects %>
<% end %>
Valery Kvon
  • 4,438
  • 1
  • 20
  • 15
  • Just for clearness... Why nested layouts? You can always use common layouts, but then you have to repeat "application" layout (with doc tags, scripts, meta tags and other stuff). Nested layouts help you avoid this and DRY. – Valery Kvon Jan 05 '13 at 21:22
0

I suggest you take a look at the rails guides, specifically routing from the outside in.

However rails can handle this, your route would be setup in the following way:

resources :company do
       resource :projects
       resource :people
end

I assume you already have all your CRUD actions setup, then this would work. However do note, it will change your named routes.

i.e

if you were calling

projects_path

in your views, they will now become:

company_projects_path

You can see a full list of routes with the command

rake routes
ismail
  • 3,882
  • 5
  • 36
  • 47