Hoặc, đây là những gì tôi đang làm. Tôi tạo một wrapper asset_tag
có thể được sử dụng như thế này:
<%= asset_tag 'mystyle', :css %>
<%= asset_tag 'mycode', :js %>
Và sau đó tôi định nghĩa nó trong application_helper
:
module ApplicationHelper
# here is where you define your paths
# in this case, paths will be '/css/mystyle.css' and '/js/mycode.js'
def asset_path(asset, type)
return "/css/#{asset}.css" if type == :css
return "/js/#{asset}.js" if type == :js
end
def asset_tag(asset, type)
return stylesheet_link_tag asset_path(asset, type) if type == :css
return javascript_include_tag asset_path(asset, type) if type == :js
end
end
Bằng cách này bạn có thể thay đổi đường dẫn tài sản dưới mọi hình thức mà bạn muốn và nó sẽ luôn luôn tương thích về phía trước.
Tôi không chắc tại sao điều này lại bị giảm giá. Đây là một giải pháp rõ ràng hơn và ít bị hacky hơn. – Schrockwell