Rails Asset Pipeline: What happens when we do require in application.js - ruby-on-rails

I am trying to contribute to the opensource project jquery-datatables-rails. But it puzzles me as how it works. I have basic understanding of how to create gem. It seems like the project just simplifies the path references but nothing more. The four steps listed to install are
Add to the assets group in your Gemfile:
gem 'jquery-datatables-rails'
Install the gem:
bundle install
Add the JavaScript to application.js:
//= require dataTables/jquery.dataTables
Add the stylesheets to application.css:
*= require dataTables/jquery.dataTables
But I don't understand how Rails knows where to find dataTables folder and why we use jquery.dataTables to reference the file/class. The code is easy to understand, but the file organization confuses me so much. Is there any writing on topics related to my confusion?
Thanks.
Update:
Just found this great documentation on Rails Engine: http://edgeguides.rubyonrails.org/engines.html

By default Rails asset pipeline searches for the files in app/assets/, lib/assets, and vendor/assets of the Rails application. But this gem is a Rails engine, and it adds path of its own assets to default assets paths. It has its own vendor/assets.

Related

FullCalendar Rails Assets

I am using the fullcalendar rails gem. I just downloaded the zip file for the calendar which contains the js and css files for the fullcalendar plugin. For all the tutorials on the fullcalendar gem I have not seen anyone doenload the zip file and use these files in their code (they use the CDN links but those don't appear to be working) where is the best folder to place these zipped assets into so that I can get the desired results on my calendar?
If you use the gem you do not need download any file.
You have to:
install gem
add in the application.js file the instruction //= require fullcalendar
And to can use it.
Read the README of the gem https://github.com/bokmann/fullcalendar-rails
Because you also need moment.js
Don't install from the zip file.
Follow the instructions at https://github.com/bokmann/fullcalendar-rails#installation for installation; you will have an easy time managing the dependencies.
The steps as specified there are:
Add gem 'fullcalendar-rails' to Gemfile
Add gem 'momentjs-rails' to Gemfile if you want to use 2.1.1.0 or higher version of fullcalendar-rails gem.
Run bundle install to install the gems.
Add //= require fullcalendar (and //= require moment) in application.js
Add *= require full calendar in application.css

Google Polymer with rails 4

I have build a Ruby on rails app.I want to use polymer with my ruby on rails app. Cam anybody suggest some good resources to learn polymer with rails ?
Is it efficient to use polymer with ruby on rails ?
Please also suggest other better options than polymer, if any ?
I am using bower, so I expect you already have bower setup on your system.
(brew install node && npm install bower)
Set up you gems
gem 'bower-rails'
gem 'emcee'
bundle install
rails g bower_rails:initialize
rails g emcee:install
Setup your bower file. Below is what mine looks like:
asset 'angular'
asset 'angular-route'
asset 'angular-resource'
asset 'angular-mocks'
asset 'webcomponentsjs'
asset 'polymer', github: 'Polymer/polymer'
asset 'polymer-core-elements', github: 'Polymer/core-elements'
asset 'polymer-paper-elements', github: 'Polymer/paper-elements'
asset 'platform'
# Voice Synthesis and Recognition
asset 'voice-elements'
Figure out the ones you need and delete the rest.
rake bower:install
To download the components but take note that the files will be saved in vendor/assets/bower_components and not vendor/assets/components expected by emcee. So you will need to make symbolic link.
Setup symbolic links
first remove components i.e. rm -rf vendor/assets/components
then set up the symlink i.e. ln -s vendor/assets/bower_components vendor/assets/components
Now setup your assets
# app/assets/components/application.html
*= require polymer/polymer
# app/assets/javascripts/application.js
//= require angular/angular
//= require angular-route/angular-route
//= require angular-resource/angular-resource
//= require webcomponentsjs/webcomponents
//= require platform/platform
(Please note that I only included all I am using for testing. As stated before, remove those you don't need).
Just some house cleaning you jump off to start trying things out. Include Bower components in rake assets path.
# config/application.rb
config.assets.paths << Rails.root.join("vendor","assets","bower_components")
And that is about it for setup. Jump on it and start using it.
Include html import tag in your layout file
= html_import_tag "application", "data-turbolinks-track" => true
And call a sample polymer in your view
# sample.html.haml
================
%paper-tabs{:selected => "0", :scrollable => ""}
%paper-tab Skills
%paper-tab Experiences
%paper-tab Education
Ref
http://angular-rails.com/bootstrap.html
http://www.akitaonrails.com/2014/06/29/usando-polymer-com-rails#.VHcoW2SsV81
On deep searching on Google. I have found that 'emcee' is the best to use polymer with rails.
following are the options :-
nevir/polymer-rails
alchapone/polymer-rails
ahuth/emcee
also check- http://joshhuckabee.com/getting-started-polymer-ruby-rails
I've built a bower package for dealing with rails forms with polymer. It my come in handy, especially if you're going with to be building any kind of ajax forms or using nested attributes.
https://github.com/hobberwickey/polymer-rails-forms
To answer your more general question though, I've been working with Polymer and both Rails / Sinatra for a while now and it's a great tool for building out the front end of your application without relying heavily on erb / haml or other server-side templating.
You can use polymer-rails and polymer-elements-rails.
https://github.com/alchapone/polymer-elements-rails
https://github.com/alchapone/polymer-rails
I was first trying to use web components with bower-rails and emcee.
But I noticed that bower-rails isn't necessary, you just need to configure .bowerrc and bower.json then run bower install.
Also, emcee is no longer active which forces you to use an older version of sprockets.
It turns out polymer-rails has the same functionality as emcee.
Here is my current setup
Gemfile
gem 'polymer-rails'
install and initialize
bundle install
rails g polymer:install
app/views/layouts/application.html.erb
<html>
<head>
...
<%= html_import_tag 'application'%>
...
Download the bower.json file from elements.polymer-project.org and put it in the root of your app then run the command
bower install
Now your components should be in vendor/assets/components. Find all the .html files to include in the manifest. Some of the .html files are not the same name as the directory so it is a little tedious.
app/assets/components/application.html.erb
//= require polymer/polymer
//= require paper-scroll-header-panel/paper-scroll-header-panel
//= require paper-toolbar/paper-toolbar
//= require paper-tabs/paper-tabs
//= require paper-drawer-panel/paper-drawer-panel
//= require paper-icon-button/paper-icon-button
//= require iron-icons/iron-icons
//= require paper-card/paper-card
//= require paper-button/paper-button
...
Now make sure sprockets loads the assets
config/initializers/assets.rb
Rails.application.config.assets.paths << Rails.root.join('vendor', 'assets', 'components', '*')
Here is an example of how to use a component
<paper-card heading="Paper Card Example" image="<%= image_path 'example.jpg' %>">
<div class="card-content">This is an example of cards with polymer</div>
<div class="card-actions">
<paper-button raised>OK</paper-button>
</div>
</paper-card>
You can use polymer-elements-rails which has a lot of the components already and you can skip the bower.json, bower install, and load assets steps. The only problem is I had a hard time finding out what the require paths were and some of the components were missing (like google-apis). Hope this helps anyone trying to use polymer with rails!

How to use typeahead.js as a Bower component with Rails 4

I'm trying to use typeahead.js in my Rails 4 app. I would like to use Bower to do this instead of using the gem. I have installed it in vendor/assets/components. I have also included the line below in config/application.rb.
config.assets.paths << Rails.root.join('vendor', 'assets', 'components')
I have searched extensively but cannot seem to figure out what exactly I should include in app/assets/javascripts/application.js to get Rails to recognize this. The directory typeahead.js is installed properly in vendor/assets/components. I have tried
//= require typeahead.js
as well a few other options but it's not working. I'm a relative newbie to Rails and am still trying to figure out how some of the things work under the hood. Any help is appreciated.
I would recommend that instead you use Rails Assets. Getting started is as simple as adding
source 'https://rails-assets.org' do
gem 'rails-assets-bootstrap' #example
gem 'rails-assets-angular' #example
gem 'rails-assets-leaflet' #example
end
to your Gemfile, replacing the examples with bower assets you need. Simply prefix a given Bower asset with "rails-assets-", and you're good to go.
Be sure to check out their website if you need any help getting started!
Your bower.json file should specify a "typeahead" dependency like this:
"vendor": {
"name": "bower-rails generated vendor assets",
"dependencies": {
"typeahead": "git#github.com:twitter/typeahead.js.git"
}
}
Run rake bower:install, it should place the library in vendor/assets/bower_components
Then in your application.js
//= require typeahead
I did not use typeahead.js after I posted my question, but need to use it now and got it to work. First of all I didn't need to add anything to config/application.rb since the vendor/assets directory is already included in the asset pipeline.
Bower installed the directory typeahead.js in the vendor/assets/components directory as I put
{
"directory": "vendor/assets/components"
}
in my .bowerrc file.
And in my app/assets/javascripts/application.js I include the full path to the typeahead.jquery.min.js file:
//= require typeahead.js/dist/typeahead.jquery.min.js
Everything works fine now.

Installing Bootstrap 3 on Rails App

I'm trying to install Bootstrap 3.0 on my Rails app. I recently finished Michael Hartl's tutorial and am now trying to build my own system using this new version of Bootstrap, but I have a few questions that I'm not sure about.
My system specs:
OS X Mountain Lion on MBP
Rails 4.0
Ruby 2.0
Questions I have:
What is the best gem to use in my Gemfile? I have found a few of them.
What do I import on my custom.css.scss? I read somewhere that it's different from 2.3.2.
Is there anything else I have to do to get Bootstrap to work, or are the remaining steps identical to the ones I followed for Bootstrap 2.3.2?
Edit
Here is what the bootstrap-rails project on GitHub first says to do:
gem 'anjlab-bootstrap-rails', :require => 'bootstrap-rails',
:github => 'anjlab/bootstrap-rails'
Then it says to do:
gem 'anjlab-bootstrap-rails', '>= 3.0.0.0', :require => 'bootstrap-rails'
Do they do the same thing, or do you have to do them both?
Actually you don't need gem for this, here is the step to install Bootstrap 3 in RoR
Download Bootstrap
Copy:
bootstrap-dist/css/bootstrap.css and bootstrap-dist/css/bootstrap.min.css
To: vendor/assets/stylesheets
Copy:
bootstrap-dist/js/bootstrap.js and bootstrap-dist/js/bootstrap.min.js
To: vendor/assets/javascripts
Update: app/assets/stylesheets/application.css by adding:
*= require bootstrap.min
Update: app/assets/javascripts/application.jsby adding:
//= require bootstrap.min
With this you can update bootstrap any time you want, don't need to wait gem to be updated. Also with this approach assets pipeline will use minified versions in production.
As many know, there is no need for a gem.
Steps to take:
Download Bootstrap
Direct download link Bootstrap 3.1.1
Or got to http://getbootstrap.com/
Copy
bootstrap/dist/css/bootstrap.css
bootstrap/dist/css/bootstrap.min.css
to: app/assets/stylesheets
Copy
bootstrap/dist/js/bootstrap.js
bootstrap/dist/js/bootstrap.min.js
to: app/assets/javascripts
Append to: app/assets/stylesheets/application.css
*= require bootstrap
Append to: app/assets/javascripts/application.js
//= require bootstrap
That is all. You are ready to add a new cool Bootstrap template.
Why app/ instead of vendor/?
It is important to add the files to app/assets, so in the future you'll be able to overwrite Bootstrap styles.
If later you want to add a custom.css.scss file with custom styles. You'll have something similar to this in application.css:
*= require bootstrap
*= require custom
If you placed the bootstrap files in app/assets, everything works as expected. But, if you placed them in vendor/assets, the Bootstrap files will be loaded last. Like this:
<link href="/assets/custom.css?body=1" media="screen" rel="stylesheet">
<link href="/assets/bootstrap.css?body=1" media="screen" rel="stylesheet">
So, some of your customizations won't be used as the Bootstrap styles will override them.
Reason behind this
Rails will search for assets in many locations; to get a list of this locations you can do this:
$ rails console
> Rails.application.config.assets.paths
In the output you'll see that app/assets takes precedence, thus loading it first.
This answer is for those of you looking to Install Bootstrap 3 in your Rails app without using a gem. There are two simple ways to do this that take less than 10 minutes. Pick the one that suites your needs best. Glyphicons and Javascript work and I've tested them with the latest beta of Rails 4.1.0 as well.
Using Bootstrap 3 with Rails 4 - The Bootstrap 3 files are copied into the vendor directory of your application.
Adding Bootstrap from a CDN to your Rails application - The Bootstrap 3 files are served from the Bootstrap CDN.
Number 2 above is the most flexible. All you need to do is change the version number that is stored in a layout helper. So you can run the Bootstrap version of your choice, whether that is 3.0.0, 3.0.3 or even older Bootstrap 2 releases.
Twitter now has a sass-ready version of bootstrap with gem included, so it is easier than ever to add it to Rails.
Simply add to your gemfile the following:
gem 'sass-rails', '>= 3.2' # sass-rails needs to be higher than 3.2
gem 'bootstrap-sass', '~> 3.1.1'
bundle install and restart your server to make the files available through the pipeline.
There is also support for compass and sass-only: https://github.com/twbs/bootstrap-sass
I use https://github.com/yabawock/bootstrap-sass-rails
Which is pretty much straight forward install, fast gem updates and followups and quick fixes in case is needed.
gem bootstrap-sass
bootstrap-sass is easy to drop into Rails with the asset pipeline.
In your Gemfile you need to add the bootstrap-sass gem, and ensure that the sass-rails gem is present - it is added to new Rails applications by default.
gem 'sass-rails', '>= 3.2' # sass-rails needs to be higher than 3.2
gem 'bootstrap-sass', '~> 3.0.3.0'
bundle install and restart your server to make the files available through the pipeline.
Source: http://rubydoc.info/gems/bootstrap-sass/3.0.3.0/frames
For me, the simplest way to do this is
1) Download and unzip bootstrap into vendor
2) Add the bootstrap path to your config
config.assets.paths << Rails.root.join("vendor/bootstrap-3.3.6-dist")
3) Require them
in css *= require css/bootstrap
in js //= require js/bootstrap
Done!
This methods makes the fonts load without any other special configuration and doesn't require moving the bootstrap files out of their self-contained directory.
Using this branch will hopefully solve the problem:
gem 'twitter-bootstrap-rails',
git: 'git://github.com/seyhunak/twitter-bootstrap-rails.git',
branch: 'bootstrap3'
I think the most up to date gem for the new bootstrap version is form anjlab.
But I don't know if it currently works good with other gems like simple_form when you do rails generate simple_form:install --bootstrap, etc. you may have to edit some initializers or configurations to fit the new bootstrap version.
I actually had an easy workaround on this one in which I nearly scratch my head on how to make it work. hahah!
Well, first I downloaded Bootstrap (the compiled css and js version).
Then I pasted all the bootstrap css files to the app/assets/stylesheets/.
And then I pasted all the bootstrap js files to the app/assets/javascripts/.
I reloaded the page and wallah! I just added bootstrap in my RoR!

How can I add the Flat UI into Rails?

Rails n00b here and I'm just wondering if anyone can help me get the DesignModo Flat UI (free) into the assets pipeline? Do I first have to download and load Bootstrap into the pipeline?
I have found a way to get Bootstrap into rails, which is fairly simple, but I added in the Flat UI files and it just seemed to screw things up. Trying to start over now.
You could use this gem
flatui-rails with javascript
Add to your Gemfile
gem 'flatui-rails'
run bundle install
Add to your application.css
*= require flat-ui
Add to your application.js
//= require flat-ui
flat-ui-rails without javascript
add to your Gemfile
gem "flat-ui-rails"
run bundle install
Add flat-ui to your application.css
*= require flat-ui
or application.css.scss
#import "flat-ui";

Resources