I get tired of hunting through a hierarchy of folders and files, from the views to the public folder, to locate a certain CSS or Javascript file. It’d be convenient to have them right with the markup, but embedding these definitions within your HTML markup is a bad idea for several reasons. For our current project, I proposed we put everything in the view directory so they are easy to find:
app/
views/
home/
index.html.rb
index.css
index.js
The convention is clear: to add page-specific CSS code, just create a new file with the same name as the view, in the same folder. Easy to add, edit and remove. The alternative (using the public folder), usually leads to a parallel hierarchy and the inconvenience of that.
I also thought, that this being Rails and all, the files should be included automatically. It turned out to be pretty easy.
The Implementation
Serving the Assets
To serve this file, we need a route and a controller.
First the route:
map.connect /asset/:path,
:controller => asset, :action => iserve_asset, :path => /.*.(js|css)/
We need to be careful here, since we’re going to be serving files directly out of our view hierarchy– make sure the route only picks up the javascript and CSS files.
The corresponding controller:
class AssetController < ApplicationController
def serve_asset
path = params[:path]
format = path.sub(/.*.(w+$)/, 1)
respond_to do |format|
format.js { render :file=>"#{RAILS_ROOT}/app/views/#{path}"}
format.css { render(:file=>"#{RAILS_ROOT}/app/views/#{path}")}
end
end
Now the assets mentioned above are served as /assets/index.css and /assets/index.js.
Original Source:http://blog.carbonfive.com/2009/03/javascript-ajax/convenient-css-and-javascript-in-ruby-on-rails
Related Stuff
-
MooV: Using cutting edge Video phones and Software Video Phones - coupling all that with VoIP and empowering the disabled.
-
Moo Telecom: VoIP communications made easy - Ring anyway with the fun and ease of using a normal phone
-
TagR:Mobile Social Network with Real Time Locations Based services, and Ambience Intelligence, VoiP, IM, Skype, Googletalk, Mapping, Flickr, Events, Calendaring, Scheduling, SecondLife Support
-
ClearSMS : ClearSMS is a Web-based application that lets you send bulk SMS messages to your customers, contacts, or just about anyone.
-
Jajah:jah is a VoIP (Voice over IP) provider, founded by Austrians Roman Scharf and Daniel Mattes in 2005[1]. The Jajah headquarters are located in Mountain View, CA, USA, and Luxembourg. Jajah maintains a development centre in Israel.
-
Skype: It’s free to download and free to call other people on Skype. Skype the number one voice over ip software
- PrivatePhone: a free local phone number with voicemail and messages you can check online or from any phone.
