Question

I am attempting to render a partial on a single page defined within application.html.erb.

This should be specific to the URI http://website.com/ (root or index if you will).

routes.rb

has root :to => static_pages#home

application.html.erb shows

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

I would like to render << layouts/homepage_slider >> on / or my index path only. It needs to be outside for the <div class=container"> otherwise I would have added it to the static_pages/home file specifically.

Was it helpful?

Solution

You can wrap an if/else statement around it.

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

or

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

OTHER TIPS

I had this same challenge when working with Rails 6 in Ubuntu 20.04.

For me, I needed to render a partial in the head tag of my app/views/layouts/application.html.erb to show up on a single page which was the app/views/products/show.html.erb.

Here's how I did it:

I created the partial in 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}" %> />

And then I included it in the head tag of my app/views/layouts/application.html.erb to show up on the app/views/products/show.html.erb page.

<!DOCTYPE html>
<html>
  <head>
.
.
.

    <%= render partial: '/shared/facebook_metatags' if params[:controller] == 'products' && params[:action] == 'show' %>
.
.
.
  </head>
</html>

That's all.

I hope this helps

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top