0.07
No release in over 3 years
Low commit activity in last 3 years
There's a lot of open issues
coffeebeans was developed by: markbates
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

 Project Readme

CoffeeBeans

Rails 3.1 has made quite a splash with its inclusion of CoffeeScript. I for one am very excited about being able to write JavaScript in an easier and more beautiful way. However, when CoffeeScript was added to Rails 3.1 they forgot one very important part, the ability to use it when responding to JavaScript (JS) requests!

In Rails 3.1 it’s incredibly easy to build your application’s JavaScript using CoffeeScript, however if you fire off an AJAX request to your application you can only write your response using regular JavaScript and not CoffeeScript, at least until CoffeeBeans came along.

Installation

Installing CoffeeBeans is simple. In your Rails 3.x project (yes, this works in Rails 3.0.x as well as Rails 3.1) add the following to your Gemfile:

gem 'coffeebeans'

Then run:

$ bundle install

That’s it! CoffeeBeans is now installed and ready to help you make the most of CoffeeScript in your Rails app. Enjoy!

Usage

Usage of CoffeeBeans is incredibly simple. Let’s say you want to respond to a JavaScript (JS) request using CoffeeScript just name your view something like this:

app/views/users/index.js.coffee

That’s it! You’re done. Now you can use CoffeeScript (and ERB) in that file. Nothing else special to do, tweak or change.

But Wait, There’s More!

Act now and we’ll throw in two view helper methods to help make it easy for you to write CoffeeScript in your views as well:

coffee_script_tag

The coffee_script_tag helper method will compile your CoffeeScript code and place it between script tags:


<%= coffee_script_tag do %>
  alert 'coffee script is awesome!'
<% end %>

Will generate:


<script type="text/javascript">
  (function() {
    alert('coffee script is awesome!');
  }).call(this);
</script>

coffee_script

Alternatively, if you want to place/compile CoffeeScript between script tags that are already on your page, then you can simply use the coffee_script helper to do that.


<script>
  <%= coffee_script do %>
    alert 'coffee script is really awesome!'
  <% end %>
</script>

Will generate:


<script>
  (function() {
    alert('coffee script is really awesome!');
  }).call(this);
</script>

Footnote

I really hope that this gem doesn’t have to be around too long and that the Rails team decides to actually add this into Rails proper. It seems like a no-brainer as far as I’m concerned. I did submit a pull request but it wasn’t accepted because they may want to refactor the rendering system in the future.