Implementing Coffeescript into Rails Application - ruby-on-rails

I have a CoffeeScript that I am trying to add into my current rails application. Once the script is in the assets/javascript folder how do I actually make it display on the page? I've gone through a bunch of tutorials and am still completely lost. Do I need to make a new controller?

Coffeescript is included as a default gem in Rails. Confirm by checking in your Gemfile.
Coffeescript is a language that compiles, so it won't 'display' on a page -- when you make a .coffee file, Rails will compile it to JavaScript when the page is loaded (or before, depending on how you do asset compilation). A coffeescript page is included the same way a js page is included (open your application.js page -- it'll have a comment explaining how to include JS).

Related

Rails vendor asset templates

I was wondering if there was a good solution for getting vendor html templates into the rails asset pipeline. Right now I'm making it work by putting the two templates I need in /public
I used Bower to install angular-ui-bootstrap and I can require the javascript fine from application.js after adding the config.assets.path in application.rb.
How do you do the same for the html templates that the angular module needs? The JS is in /src, the template is in /template.
Not sure I understand the question. You can add them as template strings js in assets/javascripts but it could get a little messy.

Where can i find the chosen.jquery.js file in my rails app?

I'm guessing this is a silly question, but i'm a bit of a newb...
I've added the chosen gem to my rails app by adding it to my Gemfile and requiring chosen-jquery in my application.js file.
My question is: where can i find the actual javascript file for chosen? Is it downloading it automatically?
Simply include the JS and CSS files like described in the documentation on the main page of the Github project and it will be included when precompiling your assets. The files are not within your project directory but rather within the gem and will be resolved by the pipeline when the gem is included.
If you need to get to the file directly (for modifications), you need to put the JS in there manually. You can then still include it in the main application.js for the pipeline and have better control of the version in use. To me, this is the preferred method.
However, may I suggest switching to Select2 which is originally based on Chosen but under much more active development and better documentation:
http://ivaynberg.github.io/select2/
https://github.com/ivaynberg/select2
There's also a gem for it if you like:
https://github.com/argerim/select2-rails
It's in a gem itself. If you look at vendor/assets/javascripts in gem root, you'll see all javascripts that come with chosen gem. They are added by assets pipeline, with
//= require chosen-jquery
line in your application.js.
Look up your assets load path. Like this:
in terminal cd to your app root
then open rails console
then run Rails.application.config.assets.paths
Your chosen-jquery is in one of those folders.

Ruby on Rails create a dynamic asset

I'm making a small gem to help me out with further development which creates a small javascript function based on some database resutls. That piece of javascript must be added to the page somehow but I'm not sure of the best way of doing it.
Do the assets pipeline have some magic code that I can add javascript/css code to it somehow? I checked the docs but found nada yet.
In your Gemfile:
place your js file in vendor/assets/javascripts
In application
add to file app/assets/javascripts/application.js lines:
#= require your_js_file_name
if you have dynamic pieses of code add extension .erb to your js file
for example: your_js_file_name.js.erb

Coffeescript import and organization for Rails

I'm building a Rails application that is almost entirely CoffeeScript upfront. The user will never wait for a page redirect because literally everything happens on the front page (a la Twitter).
Now I've had to put all of my CoffeeScript into one file because my jQuery onLoad is complex and, while in production it will be compiled to one JS file, in testing this is not the case.
How can I either import CoffeeScript from other files in development or organize my CoffeeScript so I'm not piling 500 lines into one file (when those 500 lines contain 5/6 distinct domains)?
I have used import in SCSS to do the same thing for my stylesheets, but CS is proving a bit trickier.
The asset pipeline allows you to require different files into one file, which keeps them separate in development and compiles them to one file in production. Check out the instructions on Manifest files and Directives
Also, have you looked into RequireJS? It can work in or outside of Rails and might be the sort of thing you need.

Coffeescript and Haml with unobtrusive Javascript (data-remote) in Rails 3.1

I searched le interwebs, but I haven't found someone experiencing the same problem as me, so I propose my question here.
I just started using Rails 3.1 with Compass, Haml and CoffeeScript and ran into a problem. When I rename my controller-specific JavaScript file located in app/assets/javascript/index.js to index.js.coffee and translate the JavaScript code to CoffeeScript, everything works as expected - the file is requested by the browser and compiled on the fly into JavaScript. Changes in the CoffeeScript file do also trigger recompilation.
However, when I try to do this with unobtrusive JavaScript (:remote => true) and rename the already working JavaScript file located in the view folder app/views/index/index.js.haml to index.js.coffee.haml and translate the included code, Rails doesn't recognize it as a CoffeeScript that needs to be compiled.
What am I doing wrong? Do I actively have to enable CoffeeScript evaluation for the view? Where?
The asset pipeline introduced in Rails 3.1 will automatically compile coffeescript assets into javascript for you, as you've mentioned. This is currently NOT the case for views: only files in app/assets/javascripts will be compiled.
To use Coffeescript in views, you'll need to use https://github.com/markbates/coffeeBeans for the time being.
Update : per Kyle Heironimus' comment below, it seems this functionality can be provided by coffe-rails (see https://github.com/rails/coffee-rails and http://rubygems.org/gems/coffee-rails )

Resources