4

Say that I have pulled a fancy.js file from somewhere online, what are the steps that I should take to include it in my Rails 4 application?

Right now I have the following

  1. Move fancy.js to assets/javascripts folder
  2. add //= require fancy to application.js asset pipeline
  3. ???

Are there more steps after 1 and 2, or am I doing this all wrong to begin with?

2 Answers 2

6

Looks like you are on the right path. Take a look inside app/assets/javascripts/application.js

When you created a new app (i.e., rails new app_name), it should have added

//= require_tree .

Simply add references to additional javascript files you wish to include.

EXAMPLE

//= require jquery
//= require jquery-ui
//= require jquery_ujs
//= require fancy
//= require_tree .

You can find more info on Adding Javascript File to Rails

Sign up to request clarification or add additional context in comments.

5 Comments

thanks for the reply. Are there any special syntax convention that I should be aware of? what if the file name is fancy.min.js as oppose to fancy.js? is the syntax simple require fancy.min ?
you'll have to add the file like so ://= require fancy.min.js
I see, thanks! The reason I'm asking is because I intend to use a jquery.backstretch.min.js file in my rails app, so I would have to add //= require jquery.backstretch.min.js to my application.js right?
thanks! do you happen to know if I need to do any additional steps other than declaring the js syntax if I only want to use jquery.backstretch.min.js in, let's say, sessions/new.html.erb?
just add this to your page: <%= javascript_include_tag "my_javascipt_file" %>
3

A better alternative when using popular third-party assets is to look on Rubygems for a gemified version of the asset. In this case fancybox2-rails.

gem 'fancybox2-rails', '~> 0.2.8'

Adding the gem to your application.js with sprockets:

//= require jquery
//= require fancybox

Or in a script tag:

<%= javascript_include_tag "fancybox" %>

And the css:

/*
 *= require_self
 *= require fancybox
 *= require_tree .
 */

The advantage of using Gemified packages is that its easier to maintain and you avoid polluting your codebase with 3rd party code. The drawback is that the gem may be several versions older than the actual software it encapsulates.

If you need to add vendor files you should probably be adding them to lib/assets and not app/assets, since it makes a clear separation between the code which actually drives your unique app and dependencies.

Another thing to remember is that assets are most often compiled at deploy time in your production environment (this is especially true on Heroku) and not per request (which would be very slow). So you cannot use conditions in your sprockets directives.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.