Search code examples
ruby-on-railsrubyhtmlruby-on-rails-4partials

Rails: Render a partial on a single page in Rails


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.


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