无法覆盖 Spree 的助手方法
Can't override Spree's helper's method
为什么我的代码没有覆盖 Spree 的代码?
app/helpers/spree/frontend_helper_decorator.rb
Spree::FrontendHelper.module_eval do
def taxons_tree(root_taxon, current_taxon, max_level = 1)
return '' if max_level < 1 || root_taxon.children.empty?
content_tag :ul, class: 'taxons-list' do
root_taxon.children.map do |taxon|
css_class = (current_taxon && current_taxon.self_and_ancestors.include?(taxon)) ? 'current' : nil
content_tag :li, class: css_class do
link_to(taxon.name, seo_url(taxon)) +
taxons_tree(taxon, current_taxon, max_level - 1)
end
end.join("\n").html_safe
end
end
end
看上面接受的答案
因此,在我根据需要向启动器添加装饰器文件后它起作用了:
spree.rb
require "#{Rails.root}/app/helpers/spree/frontend_helper_decorator.rb"
要添加所有助手装饰器,我使用此代码:
Dir["#{Rails.root}/app/helpers/spree/*.rb"].each {|file| require file }
修改后不要忘记重启服务器!
你的 application.rb
里有这个吗?
config.to_prepare do
# Load application's model / class decorators
Dir.glob(File.join(File.dirname(__FILE__), "../app/**/*_decorator*.rb")) do |c|
Rails.configuration.cache_classes ? require(c) : load(c)
end
# Load application's view overrides
Dir.glob(File.join(File.dirname(__FILE__), "../app/overrides/*.rb")) do |c|
Rails.configuration.cache_classes ? require(c) : load(c)
end
end
为什么我的代码没有覆盖 Spree 的代码?
app/helpers/spree/frontend_helper_decorator.rb
Spree::FrontendHelper.module_eval do
def taxons_tree(root_taxon, current_taxon, max_level = 1)
return '' if max_level < 1 || root_taxon.children.empty?
content_tag :ul, class: 'taxons-list' do
root_taxon.children.map do |taxon|
css_class = (current_taxon && current_taxon.self_and_ancestors.include?(taxon)) ? 'current' : nil
content_tag :li, class: css_class do
link_to(taxon.name, seo_url(taxon)) +
taxons_tree(taxon, current_taxon, max_level - 1)
end
end.join("\n").html_safe
end
end
end
看上面接受的答案
因此,在我根据需要向启动器添加装饰器文件后它起作用了:
spree.rb
require "#{Rails.root}/app/helpers/spree/frontend_helper_decorator.rb"
要添加所有助手装饰器,我使用此代码:
Dir["#{Rails.root}/app/helpers/spree/*.rb"].each {|file| require file }
修改后不要忘记重启服务器!
你的 application.rb
里有这个吗?
config.to_prepare do
# Load application's model / class decorators
Dir.glob(File.join(File.dirname(__FILE__), "../app/**/*_decorator*.rb")) do |c|
Rails.configuration.cache_classes ? require(c) : load(c)
end
# Load application's view overrides
Dir.glob(File.join(File.dirname(__FILE__), "../app/overrides/*.rb")) do |c|
Rails.configuration.cache_classes ? require(c) : load(c)
end
end