rails/rails — pull request #5187 by patrick99e99 (2012-02-27 05:15:22+00:00)
Title: added params option for asset include_tag helpers

Recently I discovered a problem when I was trying to setup a controller to dynamically render a js.erb template. I wanted to do something like this:

# application layout
javascript_include_tag "/javascripts/data.js?caller=#{controller_name}_#{action_name}"

This way I could have a controller with a data action that could do something like:
# javascripts_controller.rb
def data
case params[:caller]
when "posts"
@post_data = "post data"
when "store"
@store_data = "store data"
end
end

# app/views/javascripts/data.js.erb
var DataForMyApp = {
post_data: <%= @post_data %>,
store_data: <%= @store_data %>
};

... Anyway, when I tried to do this, it would not work and when looking at the page source, I saw the reason was the javascript include tag having an addition ".js" appended to the end of the query string:

So I have adjusted the ActionPack code so that you can do any of the following:

javascript_include_tag("/javascripts/data?foo=bar")
javascript_include_tag("/javascripts/data.js?foo=bar")
javascript_include_tag("/javascripts/data", :params => {:foo => "bar"})
javascript_include_tag("/javascripts/data?foo=bar", :params => {:foo2 => "bar2")

This behavior also carries over to the stylesheet_link_tag.