I recently attended a hackathon, and we a made a web application in rails. We were all new to rails and were not aware of the bootstrap gem, and thus we hard coded the bootstrap style sheet in the assets folder.
In an effort to learn Rails, further, I'm continuing with the app. I have deleted the .css file, and added the gem to my Gemfile. I've ran bundle install and imported the files using the '#import' command in another CSS file.
When I run the rails server and navigate to my homepage, I get an error:
Sass::SyntaxError in Pages#rootpage
Showing C:/Users/User/Documents/SoapBox/app/views/layouts/application.html.erb where line #6 raised:
File to import not found or unreadable: bootstrap.
Load paths:
C:/Users/Soham/Documents/SoapBox/app/assets/images
C:/Users/Soham/Documents/SoapBox/app/assets/javascripts
C:/Users/Soham/Documents/SoapBox/app/assets/stylesheets
C:/Users/Soham/Documents/SoapBox/vendor/assets/javascripts
C:/Users/Soham/Documents/SoapBox/vendor/assets/stylesheets
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/turbolinks-1.3.0/lib/assets/javascripts
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/jquery-rails-3.0.4/vendor/assets/javascripts
(in C:/Users/Soham/Documents/SoapBox/app/assets/stylesheets/styles.css.scss:2)
`File to import not found or unreadable: bootstrap`.
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href='http://fonts.googleapis.com/css?family=Oswald:700,300' rel='stylesheet' type='text/css'>
<%= favicon_link_tag "favicon.png", :type => "image/png", :rel => "icon" %>
<%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>
<%= csrf_meta_tags %>
I don't understand why the error is occurring.
Any help would be much appreciated!
Thanks!
SohamK
I recommend watching this: RailsCast
It mentions installing bootstrap like this:
#commandline
rails g bootstrap:install
If it doesn't work after that then try adding this to your less file.
#css NOT scss
#import "twitter/bootstrap/bootstrap";
Remember, that gem is in less. Importing less files into your scss file will break.
Update:
Since you are already using bootstrap-sass, it shouldn't have any problem. Can you paste the content of your styles.css.scss
Original:
You might need bootstrap-sass instead of bootstrap-rails gem if u want to import it to custom stylesheet.
#Gemfile
group :assets do
gem 'bootstrap-sass'
end
# app/assets/stylesheets/styles.css.scss
#import 'bootstrap';
#import 'bootstrap/responsive';
If you use want to use bootstrap-rails gem, you need to import inside sprocket in application.css
# Gemfile
group :assets do
gem 'bootstrap-rails'
end
# app/assets/stylesheets/application.css
/*
*= require bootstrap
*= require bootstrap/responsive
*/
Although this will not answer your question regarding your error, I much prefer to simply include the Bootstrap File in your 'Vendor' assets folder (in which it is common practice to put anything not coded by you in there like JQuery).
I prefer this because when Twitter-Bootstrap has an update, it is much simpler to just delete the files. Also, it is much nicer to see your CSS files in whatever editor you are using.
Add the extension .scss to application.css. If you deleted it, then add it again.
It should look like this:
#application.css.scss
/*
*
*= require_self
*= require_tree .
*/
#import "bootstrap";
...
It is VERY important to use that import in an .scss file. Otherwise it won't work.
Make sense?
Related
I can't figure out how make certain .scss files work with certain views. I removed the *= require_tree . from the application.css.scss but I'm not sure where to go from there. The stylesheet_link_tag doesn't seem to recognize .scss files. What tag can I use to import .scss files to views?
I also tried creating a different controller for the different view but after I removed the require_tree the controller doesn't use the .scss file that was automatically generated with it.
In application.css remove *= require_tree.
In application.html add:
<head>
<%= yield :stylesheets %>
</head>
And on every page render:
<% content_for :stylesheets do %>
<%= stylesheet_link_tag "your file" %>
<% end %>
The most important piece you are probably missing is that Sprockets won't serve any asset, mainly the css and js need to be declared in your asset manifest. Quoting the docs:
If you have other manifests or individual stylesheets and JavaScript files to include, you can add them to the precompile array in config/initializers/assets.rb:
Rails.application.config.assets.precompile += ['admin.js', 'admin.css', 'swfObject.js']
Use the expected resulting name (with .css extension) and not the source file (with the .scss extension).
There is a way to organize your assets based on controller.
Rails guide has a section about it.
According to it you can create app/assets/stylesheets/projects.css.scss file for your ProjectsController and include it like this:
<%= stylesheet_link_tag params[:controller] %>
When doing this, ensure you are not using the require_tree directive, as that will result in your assets being included more than once.
Action based stylesheets can be created similarly:
<%= stylesheet_link_tag "#{params[:controller]}/#{params[:action]}" %>
if the scss file is in the root of the asset folder then
add line to your view file
=stylesheet_link_tag 'your_scss_file_name'
in \config\initializers\assets.rb
add this line
Rails.application.config.assets.precompile + =% w (your_scss_file_name.css)
In my new rails project, I add bootstrap css and javascript; but it doesn't work and class aren't add to pages.
I copy files in app/assets/javascripts and app/assets/stylesheets.
I add this file in application.html.erb like below:
<head>
<title>JiraAjax</title>
<%= stylesheet_link_tag 'application' %>
<%= stylesheet_link_tag 'bootstrap.min' %>
<%= javascript_include_tag 'application' %>
<%= javascript_include_tag 'bootstrap.min' %>
<%= csrf_meta_tags %>
</head>
when I run project, I see both of file in html code in browser, but class aren't preview and doesn't work.
Other css is working, but bootstrap doesn't work.
I added pre-compile line for each file in application.rb, but the problem isn't solve.
app/assets/javascripts/application.js:
//= require jquery
//= require jquery_ujs
//= require bootstrap.min
app/assets/stylesheets/application.css:
*= require_self
*= require bootstrap-rtl.min
It's very strange for me, any one can help me?
You have to watch out for the load order of the assets that you're using. Depending on the specific situation, bootstrap should be loaded either before or after other assets.
Another issue I ran into was having either asset twice, which threw me off.
I'd suggest playing around with the order and making sure you don't have duplicate imports :) What I ended up doing was just hardcoding imports into my html.erb files instead of dealing with the asset-pipeline as a temporary workaround.
As a bit of pre-text, I am used to Rails 2 but started building a sample app to get used to Rails 4 and I am having a nightmare with Sprockets and the asset pipeline even after reading the official guide and every question I can find on SO.
My css and js are included like so in my main view file:
<head>
...
<%= stylesheet_link_tag "application", media: "all" %>
<%= stylesheet_link_tag "style-responsive", media: "all" %>
<%= stylesheet_link_tag params[:controller], :media => "all" if stylesheet_exists?(params[:controller]) %>
<!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
<%= javascript_include_tag 'html5shiv' %>
<%= javascript_include_tag 'respond.min' %>
<![endif]-->
...
</head>
....
<%= javascript_include_tag "jquery-1.10.2.min" %>
<%= javascript_include_tag "jquery-ui-1.9.2.custom.min" %>
<%= javascript_include_tag "jquery-migrate-1.2.1.min" %>
<%= javascript_include_tag "bootstrap.min" %>
<%= javascript_include_tag "modernizr.min" %>
<%= javascript_include_tag params[:controller] if javascript_exists?(params[:controller]) %>
stylesheet_exists? and javascript_exists? are helper functions as I only want to include certain files when they are needed as opposed to the rest which are needed on every page.
The first error is:
Asset filtered out and will not be served: add `Rails.application.config.assets.precompile += %w( style-responsive.css )` to `config/initializers/assets.rb` and restart your server
If I then add that to assets.rb and restart, it moves on and the issue is repeated for every stylesheet_link_tag and javascript_include_tag in my view.
I could live with that even if it doesn't seem right but this comes crashing down when the interpreter gets to
<%= javascript_include_tag params[:controller] %>
Would I then need to also include every js file that I create for each controller? This seems wrong that I would have to constantly update the file whenever I create a new asset file.
In my application.css.erb I only have
*= require_self.
but I do have the old fashioned CSS #import for some stylesheets:
#import url(http://fonts.googleapis.com/css?family=Open+Sans:400,300,300italic,400italic,600,600italic,700,700italic,800,800italic);
#import url('bootstrap.min.css');
#import url('bootstrap-reset.css');
#import url('jquery-ui-1.10.3.css');
#import url('<%= asset_path 'css/font-awesome.min.css' %>');
#import url('custom-ico-fonts.css');
but all of these seem to work fine and don't need to be mentioned in assets.rb.
I have no requires set in application.js.
TLDR I think I am using Sprockets and the Asset Pipeline incorrectly, could someone please point out what it is I'm doing wrong and point me in the right direction?
I read about some similar issues to do with the sprocket-rails gem version 2.2.3 but I have 2.2.4 installed which is meant to have fixed any problems that existed in the previous version.
Rails 4.2.1
ruby 2.1.0p0 (2013-12-25 revision 44422) [x86_64-linux]
sprockets (3.0.1, 2.12.3)
sprockets-rails (2.2.4)
Sprockets wants to compact everything into a single file and minify it, so typically the pattern is to include all your javascript/stylesheets in the application manifests. There are plenty of good reasons for this pattern.
Since it appears you have removed the = require_tree . lines, this is not happening automatically and thus Rails wants to know about each individual file you plan to include separately via the assets.precompile configuration option. If requiring the entire directory is too aggressive, break your assets into subfolders and be more explicit about what you want to include.
Sprockets is relatively brittle and fighting against it is an uphill battle. So in both cases (JS/CSS), you should include it all and use selectors that are smart enough to scope page-specific styles or JS to that page.
That said, sometimes we cannot avoid having certain assets omitted from global inclusion. If you need to exclude a specific file from your manifest use a stub directive and include it separately with the HTML tag helpers.
Also note that you can have additional manifests (other files that include = require ... directives of their own) ... so long as you add those to the assets.precompile list.
The combination of stub and require directives and additional manifests should give you enough flexibility to organize your assets to your liking and make adding additional assets frictionless.
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
Im very new to ruby, learning from some tutorials in a book i bought. I'm aware the books going to date quick and i think possibly already has.
I'm trying to get the follow code to load my stylesheets and my js to the page.
<!DOCTYPE html>
<html>
<head>
<title>Depot</title>
<%= stylesheet_link_tag "application" %>
<%= javascript_include_tag :defaults %>
<%= csrf_meta_tags %>
</head>
<body>
<%= yield %>
</body>
</html>
However this is not working, when i load my rails server and locally view my app from the tut, the css and js are both not loading. There is no reference to them in the head. Is this a date way to do it or perhaps something else is wrong? Any help would be appreciated, like i said im totally new and not a dev by trade so go easy on me.
Regards
V
Using rails 1.9.3-p0
First of all I advise you to look for information on asset pipeline for rails 3 ( you can start with Railscast)
To load your js files change
<%= javascript_include_tag :defaults %>
to
<%= javascript_include_tag "application" %>
and your application.js should be sth like
// This is a manifest file that'll be compiled into including all the files listed below.
// Add new JavaScript/Coffee code in separate files in this directory and they'll automatically
// be included in the compiled file accessible from http://example.com/assets/application.js
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
//
//= require jquery
//= require jquery_ujs
//= require_tree .
the last line loads your js files from assets folder