简体   繁体   English

如何为在 Rails 中的父资源中显示为选项卡的嵌套资源设置视图?

[英]How do I setup the views for nested resources that are displayed as tabs within their parent resource in Rails?

I have a resource Company that has many Projects and People .我有一家拥有许多ProjectsPeople的资源Company The index action for the companies displays each company in a table.公司的index操作在表中显示每个公司。 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". show操作显示了一些关于它的附加信息,然后我想显示的是另一个表,带有“项目”和“人员”选项卡。

When I click on the "People" tab, I should go to URL "companies/:id/people", and likewise for the "Projects" tab.当我单击“人员”选项卡时,我应该转到 URL“公司/:id/人员”,同样转到“项目”选项卡。 I'm not worried about AJAX or pre-loading all of this information into the @company variable during the show action.我不担心 AJAX 或在show操作期间将所有这些信息预加载到@company变量中。 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.现在,当我在“公司/:id/人”时,它将使用PeopleController#index ,但我想显示该视图(我想这只是它的表?)嵌套在公司的show视图中。 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.因此,当我在“公司/:id/人”和“公司/:id/projects”之间切换时,唯一改变的是表格,而不是外部的公司信息。

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.如果 Rails 不能轻松处理这类事情,我不介意使用其他东西。 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.我只是对视图层没有太多经验,所以我对它了解不多,因为我主要使用 JSON。

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 %>

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:但是 rails 可以处理这个问题,您的路线将按以下方式设置:

resources :company do
       resource :projects
       resource :people
end

I assume you already have all your CRUD actions setup, then this would work.我假设您已经设置了所有 CRUD 操作,那么这将起作用。 However do note, it will change your named routes.但是请注意,它会更改您的命名路线。

ie IE

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM