how to render partial on everything except a certain action
Asked Answered
Q

3

28

I have a _header.html.erb partial which is where I put my navbar

on my launch page I don't want to display the navbar.

this is the body of application.html.erb

<body>
<%= render 'layouts/header' %>
<div id="container">
    <%= yield %>
</div>

</body>

How do I render it on every action except specific actions on specific controllers?

Querida answered 15/11, 2012 at 10:3 Comment(0)
E
66

Replace your render with this:

<%= render 'layouts/header' unless @disable_nav %>

Then you can simply set disable_nav to true in any controller action you like:

def landing_page
  @disable_nav = true
end

As a before_action, which I'd encourage over the above:

application_controller.rb

def disable_nav
  @disable_nav = true
end

my_controller

before_action :disable_nav, only: [:landing_page]
Eumenides answered 15/11, 2012 at 10:49 Comment(2)
You can also do =render partial: "layouts/header" unless current_page?(landing_page_url)Hildegard
use before_action instead of before_filter with Rails > 4Shutt
B
7

You can put that logic in your stylesheets, in your controller or in your views (this last one, only for whole controllers).

Stylesheets

If you want to add the logic in your stylesheets, first add to your body tag the following classes:

<body class="<%= "#{controller.controller_name} #{controller.action_name}" %>">

Then, in your css, add something like this:

body.controller.action .navbar {
  display: none;
}

Controller

To add this logic to your controller, add a before filter to your application controller:

class ApplicationController < ActionController::Base
  before_filter :show_navbar

  protected
  def show_navbar
    @show_navbar = true
  end
end

Then, if you don't want to show the navbar in CarsController, do this:

class CarsController < ApplicationController
  skip_before_filter :show_navbar, only: [list, of, actions]
end

where [list, of, actions] are the actions you don't want to show the navbar in.

Finally, change you layout to look like this:

<% if @show_navbar -%>
  <%= render 'layouts/header' %>
<% end -%

Views

If you want to disable the header for whole controllers, first, move the header to app/views/application/ and change your render to:

<%= render partial: 'header' %>

Finally, in those controllers without navbar, add an empty _header.html.erb to app/views/controller_name.

For this option to work, you need at least Rails 3.1

Brabble answered 15/11, 2012 at 10:19 Comment(0)
D
0

I would set a different layout for those specific actions on specific controllers.

Dambro answered 15/11, 2012 at 10:18 Comment(4)
Given that there is so much repetition (all but a header) I wouldn't consider it.Brabble
you can have a if condition around the header partial that would run every time by default except for the specific condition.Dambro
Then that's not a different layout.Brabble
ya its not a different layout if you consider having a condition to check whether to have header or not.Dambro

© 2022 - 2024 — McMap. All rights reserved.