Nathaniel Rand Nathaniel Rand - 2 months ago 20
Ruby Question

Ruby / Rails - Conditional Statement to hide certain layout elements (navbar/footer) on root_path

QUESTION: For production, what would be an efficient conditional statement to hide certain layout elements (navbar/footer) on root_path?

I posted a similar question here. I apologize if it seems duplicated but this question is different in that I specifically want a conditional statement to remove a portion of the layout (not the entire file)

I hope this all makes sense, dealing with a noob here

I have tried:

if current_page?(root_path)
# your html
end


application.html.erb (updated version below)

<!DOCTYPE html>
<html lang="en">
<head>

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="keywords" content="">
<meta name="author" content="">

<!--Google Fonts-->
<link href="https://fonts.googleapis.com/css?family=Quicksand|Raleway:600" rel="stylesheet">

<%= stylesheet_link_tag 'application', media: 'all' %>
<%= javascript_include_tag 'application' %
<%= csrf_meta_tags %>

</head>
<body>

<!--Navbar/Footer-->
<% if current_page?(root_path) %>
#NAVBAR
#FOOTER
<% end %>**

<!--Javascript-->
<script>
$(document).ready(function(){
$('[data-toggle="tooltip"]').tooltip();
});
</script>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"> </script>

</body>
</html>


Updated application.html.erb

<% if current_page?(root_path) %>
<body>
<div class="container">
<div class="content">
<%= yield %>
</div>
</div>
</body>
<% else %>
<body>
<!--Header-->
<%= render 'shared/header' %>
<!--Content-->
<div class="container">
<div class="content">
<%= yield %>
</div>
</div>
<!--Footer-->
<%= render 'shared/footer' %>
</body>
<% end %>


Add navbar to app/views/shared/header.html.erb

Add footer to app/views/shared/footer.html.erb

Answer

As i can't see your application.html.erb ill give an example:

Inside your application.html.erb make 2 body's and make one show on the root page and the other one on all other pages like this:

<% if current_page?(root_path) %>
    <body>
     <div class="container">
      <div class="content">
        <%= yield %>
      </div>
     </div>
    </body>
<% else %>  
    <body>
    <%= render 'shared/header' %>
    <div class="container">
      <div class="content">
       <%= yield %>
      </div>
    </div>
    <%= render 'shared/footer' %>
    </body>
<% end %>

This is assuming that you have your footer and header as partials inside your app/views/shared folder