/ Published in: Rails
I used to directly inject javascript and CSS into my HEAD elements just like you see in all of the tutorials online and in books. Recently I decided to try a different approach, and I feel this is more in keeping with the ruby and rails "declaration" syntax. It has the added benefit of promoting unobtrusive javascript coding and factoring out page-specific CSS into its own file.
Expand |
Embed | Plain Text
[application_helper.rb] module ApplicationHelper def requires_javascript(path) content_for :page_dependencies do javascript_include_tag path end end def requires_stylesheet(path) content_for :page_dependencies do stylesheet_link_tag path end end end [my_layout.html.erb] <html> <head> <%= yield :page_dependencies %> ... </head> ... </html> [my_view.html.erb] <% requires_javascript "my-view-specific-javascript" %> <% requires_stylesheet "my-view-specific-stylesheet" %>
Comments
Subscribe to comments
You need to login to post a comment.

I also use this technique to implement Google Maps in the view using the YM4R plugin. Much more declarative than forcing a way to inject the GMap header stuff into the HEAD in a jumble with everything else.
In application_helper.rb:
def requiresgooglemaps contentfor :pagedependencies do concat GMap.header end end
Trying the code again with markdown...
end