在Rails中的单个页面上渲染部分

时间:2014-03-07 01:32:00

标签: ruby-on-rails ruby html5 ruby-on-rails-4 partials

我试图在application.html.erb中定义的单个页面上呈现部分内容。

这应该特定于URI http://website.com/(如果你愿意,可以是根或索引)。

的routes.rb

has root :to => static_pages#home

application.html.erb显示

<!DOCTYPE html>
<html>
<head>
  <title><%= full_title(yield(:title)) %></title>
  <%= stylesheet_link_tag    "application", media: "all",
  "data-turbolinks-track" => true %>
  <%= javascript_include_tag "application", "data-turbolinks-track" => true %>
  <%= csrf_meta_tags %>
  <%= render 'layouts/shim' %>
</head>
<body>
  <%= render 'layouts/header' %>
  <%= render 'layouts/homepage_slider' %>
  <div class="container">
    <%= yield %>
  </div>
  <%= render 'layouts/footer' %>
</body>
</html>

我想渲染&lt;&lt; layouts / homepage_slider&gt;&gt;仅在/或我的索引路径上。它需要在<div class=container">之外,否则我会特别将它添加到static_pages / home文件中。

2 个答案:

答案 0 :(得分:1)

你可以在它周围包装一个if / else语句。

<% if current_page?(root_path) %>
  <%= render 'layouts/homepage_slider' %>
<% end %>

<%= render "layouts/homepage_slider" if current_page?(root_path) %>

答案 1 :(得分:0)

Ubuntu 20.04 中使用 Rails 6 时,我也遇到了同样的挑战。

对我来说,我需要在val data = Seq(("BNG", "school"),("HYD", "school,res"),("MUM", "school,res,hos")).toDF("city","types") +----+--------------+ |city| types| +----+--------------+ | BNG| school| | HYD| school,res| | MUM|school,res,hos| +----+--------------+ data.withColumn("isSchool", array_contains(split(col("types"),","), "school")).withColumn("isRes", array_contains(split(col("types"),","), "res")).withColumn("isHos", array_contains(split(col("types"),","), "hos")) +----+--------------+--------+-----+-----+ |city| types|isSchool|isRes|isHos| +----+--------------+--------+-----+-----+ | BNG| school| true|false|false| | HYD| school,res| true| true|false| | MUM|school,res,hos| true| true| true| +----+--------------+--------+-----+-----+ 的head标签中渲染一个部分,以显示在app/views/layouts/application.html.erb的单个页面上。

这是我的做法

我在app/views/products/show.html.erb中创建了部分内容:

app/views/shared/_facebook_metatags.html.erb

然后将其包含在<!-- Here's an example of Facebook's Open Graph Markup --> <!-- https://developers.facebook.com/docs/sharing/webmasters --> <meta property="og:url" content=<%= "#{@product_url}" %> /> <meta property="og:type" content="article" /> <meta property="og:title" content=<%= "#{@product.name}" %> /> <meta property="og:description" content=<%= "#{@product.short_description}" %> /> <meta property="og:image" content=<%= "#{@product.image_url}" %> /> 的head标签中,以显示在app/views/layouts/application.html.erb页面上。

app/views/products/show.html.erb

仅此而已。

我希望这会有所帮助