Marc
Marc

Reputation: 3070

Two different layouts with one stylesheet for each

I have two different layouts and two different stylesheets (one for each) used by one controller/action.

How can I prevent the following error?

line #5 raised: app/assets/stylesheets/application.css has already been required

app/views/layouts/application.html.erb

2: <html>
3: <head>
4:   <title>Blog</title>
5:   <%= stylesheet_link_tag    "application" %>
6:   <%= javascript_include_tag "application" %>
7:   <%= csrf_meta_tags %>
8: </head>

The same error occurs when the other layout is loaded, but "application" is then "another_stylesheet_file_in_the_same_folder".

What I don't understand is why the application.css file was able to be loaded while the second layout has another <%= stylesheet_link_tag "another_css_file" %> line in there instead.

Could the reason be the asset pipeline which is loading both of these stylesheets? Or is it just an inheritance issue?

posts_controller.rb

class PostsController < ApplicationController
  layout :choose_layout

  def choose_layout
    current_uri = request.env['PATH_INFO']
    if current_uri.include?('diashow')
      @diashow = true
      return 'diashow'
    else
      @diashow = false
      return 'application'
    end
  end
...

Is the application layout already loaded before this code has been executed?

Is there a way to solve this by using the before_filter? Or is it just stupid what I try to do? :)

EDIT:

Rails console:

Rendered posts/index.html.erb within layouts/application (10.4ms)
Compiled application.css  (2ms)  (pid 23453)
Compiled diashow.css  (1ms)  (pid 23453)
Completed 500 Internal Server Error in 348ms

Why is it loading both of these files? It should only load one of them.

Upvotes: 0

Views: 1151

Answers (1)

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230316

Both these files probably include this command:

 = require_tree . 

This would make them recursively include each other.

These directives are "commented out" to maintain syntax correctness of js/css files. Asset Pipeline will nevertheless parse these comments and execute directives.

Upvotes: 4

Related Questions