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.
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.
Related
I have an application.css.scss file where I declare requirement for all of the vendor imports, as well as the main.scss file, which, in turn, serves as a centralised import for all of my styles. So it goes like this: application.css.scss < *=require main.scss < #import 'base/*'
And as long as the problem occurs on the first two levels everything works as expected: I get Rails error page with the backtrace of a problem which is very neat and helpful. But since I'm using first two levels merely to declare requirements\imports, problems tend to occur on the third level, in the partials of the actual Sass.
And in this case Rails processes an error and proceeds to render me a We're sorry, but something went wrong (500) page, instead of a page with the backtrace. I still can debug using the terminal output, but it's very uncomfortable. Here's the logs:
Started GET "/" for 127.0.0.1 at 2015-08-11 21:45:48 +0300
Processing by ApplicationController#index as HTML
Rendered application/index.html.haml within layouts/application (0.2ms)
Completed 500 Internal Server Error in 4073ms (ActiveRecord: 0.0ms)
ActionView::Template::Error (Undefined mixin 'some-mixin'.
(in /home/nh/test/app/assets/stylesheets/main.scss:5)):
2: <html>
3: <head>
4: <title>test</title>
5: <%= stylesheet_link_tag 'application', media: 'all' %>
6: <%= javascript_include_tag 'application' %>
7: <%= csrf_meta_tags %>
8: </head>
app/assets/stylesheets/base/_typography.scss:5:in `some-mixin'
app/assets/stylesheets/base/_typography.scss:5
app/assets/stylesheets/base:1
app/assets/stylesheets/main.scss:3
app/views/layouts/application.html.erb:5:in `_app_views_layouts_application_html_erb__133417964296302123_38393380'
The question is how can I make Rails monitor errors and show backtrace page when operating a deeper levels of Sass imports?
Firstly I'm still busy learning RoR so my understanding is limited. I created a new simple project and generated my first scaffold named 'Person'. When I start up my WEBrick server I go to localhost:3000/people I get the following runtime error:
ExecJS::RuntimeError in People#index
Showing C:/Users/Dean/Desktop/guestbook/app/views/layouts/application.html.erb where line #6 raised:
(in C:/Users/Dean/Desktop/guestbook/app/assets/javascripts/people.js.coffee)
Extracted source (around line #6):
3: <head>
4: <title>Guestbook</title>
5: <%= stylesheet_link_tag "application", :media => "all" %>
6: <%= javascript_include_tag "application" %>
7: <%= csrf_meta_tags %>
8: </head>
9: <body>
If any more clarification is needed please let me know and I'll gladly update my question.
I'm no expert but it looks like it's something to do with.. the coffeescript gem? I'm really just spitballin' here... Thanks in advance!
people.js.coffee:
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
ExecJS::RuntimeErrors are typically caused by syntax errors in your coffeescript. Check your people.js.coffee file around line 6 and make sure it is syntactically correct. If you are relatively unfamiliar with coffeescript, http://js2coffee.org/ converts between JS and coffee and might be helpful. .js.coffee files will only accept coffeescript.
I have seen lots of posts regarding precompiled assets in rails, but I don't think I see one that is related to my specific problem.
My application is complaining that aplication.css.scss is not precompiled.
From what I understand, any *.scss file should be compiled in my rails application when i run:
rake assets:precompile
I even confirmed in my public/assets folder that I can see the compiled application.css file.
However, when I try to visit the first page of my application I get the following error:
Compiled application.css (1002ms) (pid 20298)
Started GET "/" for 10.20.1.162 at 2013-01-21 12:21:38 -0500
Processing by MyController#new as HTML
Rendered business_searches/partials/_form.html.erb (16.6ms)
Rendered business_searches/new.html.erb within layouts/application (45.6ms)
Rendered commons/_documenthead.html.erb (34.0ms)
Completed 500 Internal Server Error in 139ms
ActionView::Template::Error (application.css.scss isn't precompiled):
4: <meta charset="utf-8" />
5: <title>My Company Name | <%= #page_title || 'My Company Tagline' %></title>
6:
7: <%= stylesheet_link_tag "application.css.scss", :media => "all" %>
8: <%= javascript_include_tag "application" %>
9:
10:
app/views/commons/_documenthead.html.erb:7:in `_app_views_commons__documenthead_html_erb___1078677729313377561_30352020'
app/views/layouts/application.html.erb:1:in `_app_views_layouts_application_html_erb___646383708034639720_28260100'
I was able to fix the problem by setting the following variable in my config/environments/production.rb file:
config.assets.compile = true
But according to this post, that is not recommended.
I would advice you to keep your manifest file application.css just for managing css assets , and not to prefix it with .scss . The correct syntax for application.html.erb file is :
<%= stylesheet_link_tag "application", :media => "all" %>
If you've placed some styles in your 'application.css, just create a newcss.scssfile in yourapp/assets/stylesheets` and place them there .
It's because your scss file is having a compile time error. I am quite sure that if you replace it's contents with known valid ones, it will get compiled.
You may comment it and check if it compiles.
Always use 'application.CSS' as base CSS.
Others should be separate.
Here's the error:
Processing by LandingPageController#index as HTML
Rendered landing_page/index.html.erb within layouts/application (2.1ms)
Completed 500 Internal Server Error in 49ms
ActionView::Template::Error (landing_page.css isn't precompiled):
12: <![endif]-->
13:
14: <%= stylesheet_link_tag "application", :media => "all" %>
15: <%= stylesheet_link_tag params[:controller] %>
16:
17: </head>
18: <body>
app/views/layouts/application.html.erb:15:in `_app_views_layouts_application_html_erb__3002306950342527375_29178380'
I can see that it's looking for the landing_page.css file because of line 15. What I don't understand is what is what is the best way to have this asset precompiled for production. I have tried modifying /config/application.rb:
config.assets.precompile += ['landing_page.css']
This doesn't seem right to me. I'd have to do this for every single stylesheet which would be annoying.
I added code to the application.css manifest:
*= require landing_page
This doesn't seem to work. I get the first error when I do this and don't modify the application config file.
I'm stumped as to how you can include the line
<%= stylesheet_link_tag params[:controller] %>
in your layout and have your assets precompiled for production when you run
bundle exec rake assets:precompile
I feel like I'm missing some simple trick that automatically adds the auto-generated stylesheets and javascript files to the list of files to be precompiled when you run the rake task.
Adding require landing_page to application.css does not cause landing_page to be precompiled. It means that when application.css is precompiled, the contents of landing_page.css will be included in the output.
If you are going to load them individually, ie
<%= stylesheet_link_tag params[:controller] %>
Then you will need to add them all to the list of things to precompile. You can use wildcards in that list, so if these controller specific stylesheets were all in stylesheets/controllers then you could do
config.assets.precompile += ["controllers/*.css"]
Typically though people tend not to do this. While for ease of development things are often split up on a per controller basis, all of these are then required from application.css. Application.css is then the only stylesheet you call stylesheet_link_tag on
in your /config/environments/production.rb add this:
config.assets.precompile += %w( landing_page.css)
This was news to me so I'll share: In Rails 4, only application.js|css manifest files files are auto-compiled by Rails. If you're using something like
javascript_include_tag controller_name
# ex javascript_include_tag 'guitars'
you apparently need to add your custom manifests ('guitars') to config.assets.precompile as mentioned above. I {believe} this is a change from Rails 3.2.
I am reading the "agile web development with rails" book and ran into a problem at the end of chapter 6. Basically, what I have done so far is defined a sass stylesheet (products.css.scss) and linked it to my application in layouts/application.html.erb:
<!DOCTYPE html>
<html>
<head>
<title>Depot</title>
<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
</head>
<body class='<%= controller.controller_name %>'>
<%= yield %>
</body>
</html>
but it doesn't load when i run the server and visit the page!
any idea why?
This solved my problem:
bundle exec rake assets:precompile
i just spent an hour figuring out the answer, since been having the exact same issue myself. go to your application.css.sass and make sure it has
/* ...
*= require_self
*= require_tree .
*/
in it. this auto loads all the other .css.sass in apps/assets/stylesheets, then precompiles them into public/assets/stylesheets to be 1 statis css file, and that is being served to your browser.
Have you include that products.css.scss in the application.css manifest file that you are referencing in the layout?
With the asset pipeline enabled, you must include the manifest in the layout and reference all the stylesheets from the manifest.
Hope it helps.
Running the assets precompile command will compile as the user above mentioned... however, that might not be what you want to do unless you want to have to run that everytime you make a change and then add all of this to your SCM repo and then possibly have issues in production.
The real solution to this exact example is that the doesn't have the "products" class in it, so the products.css.scss isn't picked up. See this post which helped me understand this: https://stackoverflow.com/a/10080134
place the depot.css from /public/stylesheets/ to app/assets/stylesheets/
according to the documentation (http://apidock.com/rails/ActionView/Helpers/AssetTagHelper/StylesheetTagHelpers/stylesheet_link_tag) you can set the tag to include all stylesheets in the stylesheets directory with stylesheet_link_tag :all There are also options for caching, recursion, and concatenation.
If you don't want to include everything but just the application.css and your controller's css you could do this:
= stylesheet_link_tag 'application', params[:controller].classify.downcase.pluralize