我正在将一个应用程序升级到Rails 5,#asset_path
现在会引发url是否为nil。我正试图用一个类似Rails4的版本来修补这个方法,这样我就可以通过测试了。
我花了几个小时在这上面,我快疯了。出于某种原因,无论我做什么,我都不能对模块进行猴子补丁。我以为这个初始化器会起作用:
module ActionView
module Helpers
module AssetUrlHelper
alias asset_path_raise_on_nil asset_path
def asset_path(source, options = {})
return '' if source.nil?
asset_path_raise_on_nil(source, options)
end
end
end
end
我还尝试将我的方法放在另一个模块中,并将include
ing、prepend
ing和append
分别放在ActionView::Helpers::AssetUrlHelper
和ActionView::Helpers::AssetTagHelper
中。
无论我做什么,我都无法执行我的方法。我唯一可以更改方法的方法是bundle open actionview
并更改实际方法。
我发现这是因为#asset_path
只是一个别名。我需要覆盖别名指向的方法:
module ActionView
module Helpers
module AssetTagHelper
alias_method :path_to_asset_raise_on_nil, :path_to_asset
def path_to_asset(source, options = {})
return '' if source.nil?
path_to_asset_raise_on_nil(source, options)
end
end
end
end