Sebastian Delgado Sebastian Delgado - 3 months ago 9
CSS Question

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

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.

Answer

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.controller_name != 'my_awesome_controller' %>
    <%= 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>