How to use the Bundled assets plugin with Rails' built-in tag helpers
posted: September 7th, 2007 · by: Sven
Nick Pearson has posted a great tip in the comments on how he uses the Bundled Assets plugin with Rails’ built-in Javascript and stylesheet helpers.
I myself have always been using this plugin with just hardcoded Javascript/CSS include tags … so I haven’t been aware that it’s that easy to do!
The original article is pretty long already and I guess it makes sense to post this tip as an article on its own. So here we go …
As Nick explains all you have to do to get this working is: use a slightly different route in your routes.rb than I’ve told you to do in my original article. Nick changed the route to:
map.connect ":asset_dir/:names.:ext",
:controller => "assets_bundle",
:action => "fetch",
:asset_dir => /(stylesheets|javascripts)/,
:ext => /(css|js)/,
:names => /[^.]*/
Obviously this routes a request to an URL like /stylesheets/main,content,forms.css to the AssetsBundleController#fetch action … while it preserves the stylesheets and javascripts directories - and that’s where the built-in Rails helpers point by default.
This way, you now can use these neat, little helpers like so:
<%= stylesheet_link_tag 'main,content,forms' %>
<%= javascript_include_tag 'common,forms' %>
… without bothering about any further details like HTML attributes that might be necessary here (and that I personally have to look up every time).
So, that’s pretty cool. Thanks Nick! :-)
PS:
The same thing, but using Liquid template tags, would be:
{{ 'main,content,forms' | stylesheet }}
{{ 'common,forms' | javascript }}
Nice, too. Eh?
Edgar said October 21st, 2007 at 01:53 PM ¶
Sven, I’m trying using this in my layout:
<%= javascriptincludetag ‘prototype,effects,account,application’ %>
but I got the following Javascript error:
“missing ; before statement”
the line 56 in prototype,effects,account,application.js is:
params=Hash.toQueryString(params);if(params&&/Konqueror|Safari|KHTML/.test(navigator.userAgent))params+=’&_=’if(this.method==’get’&¶ms)
(I’m using rails 1.2.3)
Any clue?
Sven said October 22nd, 2007 at 12:27 PM ¶
Hey Edgar,
this looks like you’re running into JSMin’s shortcomings. If I had to guess, I’d say that there’s a semicolon missing after ‘&_=’
You can try two things: a) check your JS libs and add semicolons everywhere at the end of the line. That seems to work in 99% of all cases. If even this doesn’t help, consider b) to deactivate JSMin by using
(see this article for more details about the options)
chris said October 23rd, 2007 at 01:31 AM ¶
Hi there.
I have defined this exact route but am getting 404s when trying to reference it directly. I defined this route towards the top of my routes.rb, and I didn’t not include the original route suggested in the original bundles_assets article. Just using the above route should work, correct? I am using nginx. I log into my server and check the public folder (and the stylesheets and javascripts folders) and do not see any .html (cache) files being created.
I simply did this:
Any suggestions? Thanks!
Edgar said October 24th, 2007 at 06:27 PM ¶
Sven,
Yes, my fault, I didn’t apply the patch to jsmin. Now works on my sandobox.
But when I deployed to our staging server, the response generated by jsmin in empty
any clue?
Sven said October 26th, 2007 at 02:10 PM ¶
Hi Chris,
I guess we’ve figured that in private email by now, but to have the answer here, too, the problem was that ngix didn’t forward requests for JS and CSS files to Rails at all. By either changing the ngix conf or using the /bundles route things should work.
Sven said October 26th, 2007 at 02:13 PM ¶
Hi Edgar,
just guessing, but do you have compiled jsmin on your staging box? Or did you just copy it over to it? You might want to try to compile it there then.
Otherwise I don’t have an idea. Try to narrow the problem by switching jsmin compacting off. (see this article for options)
Bernard said December 13th, 2007 at 11:28 AM ¶
Quick newbie question, I have public/cache folder for storing all page caches. I have installed bundled assets as prescribed. It works. However, some views perform the page caching and other do not.How should I configure routes.rb or init.rb?
Sven said December 13th, 2007 at 11:53 AM ¶
Hi Bernard,
it seems that this would be a Mephisto related question. Actually I’ve always been using the public directory itself for page caching and I’ve never had any issues with that.
My recommendation would be to ask that question on the Mephisto mailinglist or #mephisto IRC channel on irc.freenode.net.