how to render partial on everything except a certain action

前端 未结 3 1607
小蘑菇
小蘑菇 2020-12-12 23:09

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 appl

3条回答
  •  眼角桃花
    2020-12-12 23:36

    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:

    ">
    

    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

提交回复
热议问题