繁体   English   中英

在Rails中嵌套布局时,如何防止我的application.css文件包含两次?

[英]When nesting layouts in Rails, how do I prevent my application.css file from getting included twice?

我正在使用Rails 4.2.3。 嵌套布局时,如何防止将application.css.scss文件包含两次? 在我的“ app / views / layouts / application.html.erb”文件中,

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><%= content_for?(:title) ? yield(:title) : "Runtrax" %></title>
    <meta name="description" content="<%= content_for?(:description) ? yield(:description) : "Runtrax" %>">
    <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
    <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
    <%= csrf_meta_tags %>
  </head>
  <body>

    <% if !current_user.nil? %>
    <div id="container">
    <header>
      <%= render 'layouts/navigation' %>
    </header>
    <main role="main">
      <%= render 'layouts/messages' %>
      <%= yield %>
    </main>
    </div>
    <% else %>
      <%= yield %>
    <% end %>

  </body>
</html>

然后在我的用户布局app / views / layouts / user.html.erb中,

<%= javascript_include_tag "countries" %>
<%= javascript_include_tag "users" %>

<%= yield %>

<% parent_layout "application" %>

但是,当呈现我的一个用户页面时,它出现在<head>元素中……

并且在呈现用户布局时(在<main>部分中)也存在此问题……

<link rel="stylesheet" media="all" href="/assets/application.self-97ad5c0b00e27e03a2a434530385915aee55c42b8f0ae090c1a2abf4507ff7d8.css?body=1" />

我只希望它出现一次。 我需要怎么做才能做到这一点? 如果重要的话,这是我正在使用的app / helpers / layout_helper.rb模块

# Place this in app/helpers/layouts_helper.rb
module LayoutHelper
  def parent_layout(layout)
    @view_flow.set(:layout, output_buffer)
    output = render(:file => "layouts/#{layout}")
    self.output_buffer = ActionView::OutputBuffer.new(output)
  end
end

对于几种布局,我只需要在layouts文件夹中创建一个附加布局,然后在所需的控制器操作结束时执行以下操作:

render layout: "user" # for your case it is "user"

因此,实际上您不是创建嵌套布局,而是创建单独的布局。 您可以将current_user逻辑移至控制器动作中,以选择要呈现的布局。 为了使您的布局视图代码干使用部分。

以下是如何拆分布局的示例:

<!-- application layout -->
<!DOCTYPE html>
<html>
  <head>
    <%= render "layouts/head_section" %>
    <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
    <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
  </head>
  <body>

    <%= yield %>

  </body>
</html>

<!-- user layout -->
<!DOCTYPE html>
<html>
  <head>
    <%= render "layouts/head_section" %>
    <%= javascript_include_tag "countries" %>
    <%= javascript_include_tag "users" %>
  </head>
  <body>
    <div id="container">
      <header>
        <%= render 'layouts/navigation' %>
      </header>
      <main role="main">
        <%= render 'layouts/messages' %>
        <%= yield %>
      </main>
    </div>
  </body>
</html>

<!-- _head_section.html.erb -->
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><%= content_for?(:title) ? yield(:title) : "Runtrax" %></title>
<meta name="description" content="<%= content_for?(:description) ? yield(:description) : "Runtrax" %>">
<%= csrf_meta_tags %>

# controller
def some_action
  ...
  render layout: (current_user.present? ? "user" : "application")
end

但是,如果要在两种布局中都使用Turbolink,则需要考虑在应用程序布局或清单文件中仅包含必要的js和css文件,例如jquery,turbolinks和其他常用功能。 并且其余部分应包含在您的自定义布局中,因此您的布局将如下所示:

<!-- application layout -->
<!DOCTYPE html>
<html>
  <head>
    <%= render "layouts/head_section" %>
    <%= javascript_include_tag "none_users" %>
  </head>
  <body>

    <%= yield %>

  </body>
</html>

<!-- user layout -->
<!DOCTYPE html>
<html>
  <head>
    <%= render "layouts/head_section" %>
    <%= javascript_include_tag "countries" %>
    <%= javascript_include_tag "users" %>
  </head>
  <body>

    <div id="container">
      <header>
        <%= render 'layouts/navigation' %>
      </header>
      <main role="main">
        <%= render 'layouts/messages' %>
        <%= yield %>
      </main>
    </div>

  </body>
</html>

<!-- _head_section.html.erb -->
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><%= content_for?(:title) ? yield(:title) : "Runtrax" %></title>
<meta name="description" content="<%= content_for?(:description) ? yield(:description) : "Runtrax" %>">
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>

通常,您希望将布局的复杂性降至最低,并避免使用if-else逻辑,因为随着时间的流逝,这种逻辑会变得非常混乱。

暂无
暂无

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

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