问题
I want to do something like this in my asset pipeline:
application.js
//= require jquery
//= require jquery_ujs
//= require_tree ./includes
//= require global
<% if File.exists? "#{Rails.root}/assets/javascripts/#{params[:controller]}.js" %>
//= require <%= params[:controller] %>
<% end %>
<% if File.exists? "#{Rails.root}/assets/javascripts/#{params[:controller]}/#{params[:action]}.js" %>
//= require <%= params[:controller] %>/<%= params[:action] %>
<% end %>
Now this doesn't work because the assets files cannot access params
variable. Now is there a way to grab what the current controller action names are from the asset?
Or is the asset file precompiled for the whole project, rending this impossible?
回答1:
You may be able to make this possible through some Tom Foolery, but I would recommend highly against it.
The reason I say so is because you want this js resource to be cacheable, and for that to be the case it really needs to be static. Customizing the asset based on what controller is being invoked is going to result in what is a potentially a lot of unique resources all of which should be identified, downloaded and cached separately. Noting that you have included what are obviously unchanging resources such as jQuery, and pondering the possibility that these would need to be downloaded over and over again, I think the point becomes clear this approach doesn't work to well.
If what you are trying to do is customize the initialization of a view based on the controller/action, I would recommend including function definitions in the static js asset you are delivering that you can then invoke directly from the page.
Hope this helps.
回答2:
Assets will be precompiled normally, this won't be possible unless you disable it and build on each request.
If this is for loading some js that can only be on certain pages, then you can still explicitly include just that file in your views.
If you're trying to do this for performance reasons; loading 1 js file per page is generally the fastest thing to do. The whole file can be cached and each page load it saves a few extra requests. The size of the file is almost never a factor.
Usually I follow a pattern of cramming all of my js files in one, then using feature detection to enable certain javascript eg if some element is on the page then load something.
$("body#user_show").each(function() {
$(".user_info_box").each(load_user_info)
// ...
})
This way I can cache everything and only load js for things I detect on the page.
回答3:
I wouldn't include the controller name in the javascript itself since you want your assets to be cached. A better solution would be to use data attributes possibly in the tag to customize your scripts initialization process. For example,
<body data-controller="<%= controller.controller_name %>" data-action="<%= controller.action_name %>">
Then in your application.js you could invoke modules based on the controller name ie. Users.init()
来源:https://stackoverflow.com/questions/8250951/rails-is-it-possible-to-get-the-controller-and-action-name-from-an-asset-file