0.0
No commit activity in last 3 years
No release in over 3 years
This gem will provide an easy way to export your rails routes to a javascript file that you can then include in your asset pipeline.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Runtime

~> 3.2.5
 Project Readme

CoffeeGrounds¶ ↑

Purpose¶ ↑

I wrote this to prove an easy way to access your rails routes from your client side javascript. Have you ever wanted to do something like $.get( blog_post_path({id: 1}) )? Well this will let you do that!

How do I use it?¶ ↑

(1) add this gem to your Gemfile

* gem 'coffee-grounds'

(2) bundle install

(3) generate the javascript file

* rake coffee-grounds:compile-asset
  This will generate a coffee-grounds.js file in your app/assets/javascripts folder.

All of your named routes will show up under an App object that is defined on the window. To access a route all you need to do is call App.routes.<route_name_path>() This function takes optional arguments, the first optional argument is an object containing identifiers and their values, and the second argument is the requested format.

Cool additions¶ ↑

Example: You have a rails route that is: /foo/:id(.format) (named as foo) and you would like to get the path for an id of ‘bar’ and request it as html. App.routes.foo_path( { id: “bar” }, “html” ) That will return “/foo/bar.html”

I have also added easy ways to get and post data.

  • App.request.<named_route>_path() will execute a JQuery get to the specified route.

  • App.post.<named_route>_path() will execute a JQuery post to the specified route.

Both functions take the following optional arguments:

  • identifiers: an object with keys and values corresponding to your defined rails routes

  • params: an object containing the params to be sent with the request. (This is just passed down to JQuery )

  • function: this will be executed on a successful callback ( also passed to JQuery )

  • format: This format is both used in building the url, and passed to JQuery so that JQuery knows whats going on.

I also added a helper function to help init your coffee script on your pages, just include the following after your application javascript.

* haml =coffee_grounds_script_tag.html_safe
* erb <%= coffee_grounds_script_tag.html_safe %>

This will attempt to call:

* App.<controller_name>.init()
* App.<controller_name>.<action_name>.init()

You can specify the JavaScript name space by passing it in to the helper function.

Your CoffeeScript file should have a section defining your init functions for each action. It would look something like:

App.home =
 init: ->
  Bind your clicks and do some cool stuff for all actions in this controller
 index:
   init: ->
     Bind your clicks and do cool stuff for only the index.