How to prevent from loading a css and a js sheet at only one Rails controller?


Tags: javascript,css,ruby-on-rails,ruby,ruby-on-rails-4

Problem :

How can i prevent one of my Rails app Controller from loading the general css and js that is loaded on the whole app ?

Thanks for reading.



Solution :

Option 1

If page should be without any layout (which includes css and js), you can do something like this:

class MyAwesomeController < ApplicationController
  layout false
end

Option 2

If you need some layout, but without any css and js, create it in views/layouts. For example no_css_and_js.html.erb. Then add html without any css and js including:

<!-- layouts/no_css_and_js.html.erb -->
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
</head>
<body>
  <%= yield %>
</body>
</html>

Then your controller should be modified to use this layout:

class MyAwesomeController < ApplicationController
  layout 'no_css_and_js'
end

Option 3

Also you can modify your application layout to prevent it to load css and js:

<!DOCTYPE html>
<html>
<head>
  <title>Orca</title>
  <% if controller_name != 'my_awesome' %>
    <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true %>
    <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
  <% end %>
  <%= csrf_meta_tags %>
</head>
<body>

<%= yield %>

</body>
</html>

    CSS Howto..

    How to position div underneath dynamic ul

    How is physical inch related to pixels in css given so many different resolutions?

    How make text before ul to come in single line in html?

    How to make a responsive text inside a bootstrap button

    css: how to build centered div with minimum spacing on the left

    How to do drop-down box like “More action” in gmail

    How to avoid third party CSS to affect current CSS? [closed]

    How to display a div on hover of button using css

    CSS: How does youtube change its appearance based on screen size?

    Only show wordpress featured image when shared on social media

    CSS: 2-column table, how to make one column as small as possible but no wrap and the other as big as possible?

    How to only show certain parts with CSS for Print?

    How do you refer to more than one .css file in html?

    How to add a css style class atribute to links generated from wp_get_archives?

    Bootstrap: navbar showing a line

    how to make edges of a line fade out?

    How to include css and jquery in joomla component

    With CSS, how would I align this banner and navigation menu to XHTML 1.0 Strict specifications?

    Show text underneath image while hovering without disrupting other elements

    How to remove space above the
    tag?

    How do I hide the parent of a nested list while keeping the nested list visible?

    How to unfold or close content in html/JS - always assigned to different id

    (some) background images won't show up in IE7 or IE8

    How to decrease CSS opacity

    How to select the first element from a selection when one element does not have a class via CSS?

    How to override existing CSS to let child
    1 has fixed width and child
    has remaining width of parent?

    How to move this structure to the right without using float?

    How to define different css based on element changing number

    How to create desired shapes and position them accordingly using CSS, CSS3?

    How can I have a CSS style different for IE6?