How to Remove Header and Footer from Some of The Pages in Ruby on Rails

how to remove header and footer from some of the pages in ruby on rails

Controllers support :only and :except options for layouts, see the Conditional layouts section in this guide.

So you can do the following in your controller:

class SomeController < ApplicationController
layout 'application', :except => [:some_action, :some_other_action]
...

I want to eliminate header or footer from some of the pages in my rails rails app

Each page of a rails app is an action nested inside a controller.

You can see the current controller_name and action_name by adding the below to a view:

<%= controller_name %>
<%= action_name %>

To not render header or a footer for a specific page (controller or action or both) you can make the rendering conditional:

application.html.erb

<body>
<% unless controller_name = "static_pages" %>
<%= render "layouts/header" %>
<% end %>
<%= yield %>
<% unless action_name = "landing_page" %>
<%= render "layouts/footer" %>
<% end %>
</body>

Alternatively as a more sophisticated approach you can create a separate layout file for a specific controller: https://guides.rubyonrails.org/layouts_and_rendering.html#finding-layouts

How to hide footer layout on a particular page?

The easiest/quickest way would probably be to define a conditional:

<%= render "layouts/footer" unless @skip_footer %>

and then set the variable as necessary in your actions:

def non_footer_action
do_stuff
@skip_footer = true
end

How can i only show the header on some pages and not all of them

You can do this in your layout file:

<% unless @hide_header %>
<%= render 'header' %>
<% end %>

And then set @hide_header = true in your controller if you want it to be hidden.

I have a partial in application.html.erb, how do I remove it on certain views?

You can set the condition below for rendering your partial.

<%= render 'layouts/header'%>
<%= yield %>
<% unless controller_name=="sessions" && action_name=='new' %>
<%= render 'layouts/footer'%>
<%end%>

Ruby on Rails using different footer styling in each page

A quick and nasty way might be,

Creating a partial (probably you've already done)

pages/_footer.html.erb

<div class='footer <%= @position||"relative" %>'>
Lorem ipsum
</div>

Adding a private method to controller(s).

class PagesController < ApplicationController
before_action :set_page, only: [:show, :edit, :update, :foo, :destroy]
before_action :fixed_footer, only: [:show, :edit, :mission, :vision]

def index
@pages = Page.all
end

def show
end

def foo
render 'show'
end

def about
render 'some_static_page'
end

def mission
render 'some_static_page'
end

def vision
render 'some_static_page'
end

.....

private
def fixed_footer
@position = "fixed"
end
# Use callbacks to share common setup or constraints between actions.
def set_page
@page = Page.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the white list through.
def page_params
params.require(:page).permit(:title, :email, :comments)
end
end

Then referencing to it in your layout file (as I've quickly created and scaffolded, I've only application layout).

<!DOCTYPE html>
<html>
<head>
<title>Simpleapp</title>
<%= csrf_meta_tags %>

<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
</head>

<body>
<%= yield %>
<%= render 'pages/footer' %>
</body>
</html>

That's all. Actually not all, What you've to do is, to give some style to fixed and relative classes in your stylesheet file.

Results: [index, show, new, foo, edit]. 2 and 5 has fixed class.

$ curl http://lvh.me:3000/pages | grep "div class='footer"
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 2815 0 2815 0 0 18599 0 --:--:-- --:--:-- --:--:-- 18642
<div class='footer relative'>
$ curl http://lvh.me:3000/pages/1 | grep "div class='footer"
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 2388 0 2388 0 0 16032 0 --:--:-- --:--:-- --:--:-- 16135
<div class='footer fixed'>
$ curl http://lvh.me:3000/pages/new | grep "div class='footer"
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 2833 0 2833 0 0 12242 0 --:--:-- --:--:-- --:--:-- 12264
<div class='footer relative'>
$ curl http://lvh.me:3000/pages/1/foo | grep "div class='footer"
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 2391 0 2391 0 0 16165 0 --:--:-- --:--:-- --:--:-- 16265
<div class='footer relative'>
$ curl http://lvh.me:3000/pages/1/edit | grep "div class='footer"
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 2959 0 2959 0 0 19393 0 --:--:-- --:--:-- --:--:-- 19467
<div class='footer fixed'>

Inserting Headers and Footers in Ruby on Rails web application?

In the new_app/views/layouts there will be a file called application.html.erb. Open that file and put your header content above where it is written <%= yield> and footer content below <%=yield>.

I usually make a parital in layouts file called _header.html.erb and _footer.html.erb and do something like this :-

<%= render "layouts/header" %>
<%=yield %>
<%= render "layouts/footer" %>

Remove header conditionnally in active admin on rails5

I finally find a solution.
1 - Create header.rb in app/admin/

module ActiveAdmin
module Views
class Header < Component

def build(namespace, menu)
super(id: "header")

#add a class hide wich diplay none on the menu
if params['embedded'] == 'true'
super(class: "hide")
end
@namespace = namespace
@menu = menu
@utility_menu = @namespace.fetch_menu(:utility_navigation)

build_site_title
build_global_navigation
build_utility_navigation
end

def build_site_title
insert_tag view_factory.site_title, @namespace
end

def build_global_navigation
#do not insert tag if params embedded is true
insert_tag view_factory.global_navigation, @menu, class: 'header-item tabs' unless params['embedded'] == 'true'
end

def build_utility_navigation
insert_tag view_factory.utility_navigation, @utility_menu, id: "utility_nav", class: 'header-item tabs'
end

end
end
end

How to remove second footer coming from rails app because of .load in jquery/ajax?

You can do this in 2 ways. One is to specify the container from the url you want to load:

$('#div1').load(url+' #container');

So say you have a structure like:

<div id="header"></div>
<div id="container"></div>
<div id="footer"></div>

The above jquery code will just grab the contents of container and exclude everything outside. More details about this can be found here.

Or you can use the get function instead and change the html before outputting:

$.get(url).done(function(data) {
//do some stripping
$('#div1').html(data);
})


Related Topics



Leave a reply



Submit