<commit_before># asset\_pipeline\_routes

Getting your Rails routes into the Rails 3.2 asset pipeline is really easy. Just
`include Rails.application.routes.url_helpers` and you have all your routes available.

But except for hard-coded links this won't help you, because all resource links dynamic params at compile-time to work, like `{:id => 42}`. Without supplying them you won't get anywhere.

Heh, you might think! Just call a route helper and pass in a dynamic parameter mapping, like
`user_path('{{id}}')`. Sadly this won't yield the desired result! Instead of `/users/{{id}}`, you'll be presented with `/users/%7B%7Bid%7D%7D` because you're mapping just got html_escaped!

This is where asset\_pipeline\_routes comes to the rescue!

# What it does

It adds a helper object to your asset-pipeline called `r`. Using
`r` you can access all your routes you'd normally do, except that all those path fragments
can be changed to whatever you need them to.

Here's an example, assuming you got a routes.rb with

    resources :users # => yields multiple routes, e.g. /users/:id(.:format)

in it. Then, in you're javascript file you'd call `r.users_path` instead of `users_path`. 
The path fragments are replaced with Mustache-style attribute bindings by default:
    
    # application.js.coffee.erb
    userPath = '<%= r.user_path %>' # => yields /users/{{id}}
    usersPath = '<%= r.users_path %>' # => yields /users

You can even hook up member- or collection routes, whatever you like. Just prefix your routes with `r.` and you can directly use them in your view!

If you don't want the attribute binding pass in whatever argument you like and the path fragments get replaced. Just like that:

    # application.js.coffee.erb
    userPath = '<%= r.user_path '\d+' %>' # => yields /users/\d+
    
But sometimes you want to generate the URL for a given resource on the client-side entirely. That's where asset\_pipeline\_routes comes in handy as well:

    # application.js.coffee.erb
    editUserRoute = `<%= r.edit_user_method %>` # => yields anonymous function
    editUserPath = editUserRoute userId # => yields /user/<<id>>/edit
    
Now you have total control over your Rails routes.

# Addendum

If you happen to use haml\_assets to be able to use HAML in your asset pipeline, you could easily create forms to be used in Backbone.js or similar - because you can add an url option which correctly binds to your context!<commit_msg>clarification on route generation using anonymous functions<commit_after># asset\_pipeline\_routes

Getting your Rails routes into the Rails 3.2 asset pipeline is really easy. Just
`include Rails.application.routes.url_helpers` and you have all your routes available.

But except for hard-coded links this won't help you, because all resource links dynamic params at compile-time to work, like `{:id => 42}`. Without supplying them you won't get anywhere.

Heh, you might think! Just call a route helper and pass in a dynamic parameter mapping, like
`user_path('{{id}}')`. Sadly this won't yield the desired result! Instead of `/users/{{id}}`, you'll be presented with `/users/%7B%7Bid%7D%7D` because you're mapping just got html_escaped!

This is where asset\_pipeline\_routes comes to the rescue!

# What it does

It adds a helper object to your asset-pipeline called `r`. Using
`r` you can access all your routes you'd normally do, except that all those path fragments
can be changed to whatever you need them to.

Here's an example, assuming you got a routes.rb with

    resources :users # => yields multiple routes, e.g. /users/:id(.:format)

in it. Then, in you're javascript file you'd call `r.users_path` instead of `users_path`. 
The path fragments are replaced with Mustache-style attribute bindings by default:
    
    # application.js.coffee.erb
    userPath = '<%= r.user_path %>' # => yields /users/{{id}}
    usersPath = '<%= r.users_path %>' # => yields /users

You can even hook up member- or collection routes, whatever you like. Just prefix your routes with `r.` and you can directly use them in your view!

If you don't want the attribute binding pass in whatever argument you like and the path fragments get replaced. Just like that:

    # application.js.coffee.erb
    userPath = '<%= r.user_path '\d+' %>' # => yields /users/\d+
    
But sometimes you want to generate the URL for a given resource on the client-side entirely. That's where asset\_pipeline\_routes comes in handy as well:

    # application.js.coffee.erb
    editUserRoute = `<%= r.edit_user_method %>` # => yields anonymous function
    editUserPath = editUserRoute(42) # => yields /user/42/edit
    
Now you have total control over your Rails routes.

# Addendum

If you happen to use haml\_assets to be able to use HAML in your asset pipeline, you could easily create forms to be used in Backbone.js or similar - because you can add an url option which correctly binds to your context!
{'fraction_non_alphanumeric': 0.08738061369640317, 'fraction_numerical': 0.00406421459053038, 'mean_word_length': 3.9417670682730925, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '391477', 'n_tokens_mistral': 1409, 'n_tokens_neox': 1351, 'n_words': 664}