我不知道是否有这样的宝石,但是你可以重写设计用于渲染的全部位于看法和泛音输出类:
.bundle/ruby/ruby-2.3.1/gems/action-view-4.2.5.1/lib/action_view/template.rb
(更换您的版本号)
有一种方法称为def compile(mod)
不支持该方法来添加您想要在视图中呈现的自定义字符串。对于你的情况,你可以做这样的事情:
def compile(mod) #:nodoc:
encode!
method_name = self.method_name
code = @handler.call(self)
# This is the code I have added
code.insert(63, "@output_buffer.safe_append='\n<!--#{self.inspect} start -->\n'.freeze\;")
code.insert(code.size-19, "@output_buffer.safe_append='\n<!--#{self.inspect} end -->\n'.freeze\;")
# Make sure that the resulting String to be eval'd is in the
# encoding of the code
source = <<-end_src
def #{method_name}(local_assigns, output_buffer)
_old_virtual_path, @virtual_path = @virtual_path, #{@virtual_path.inspect};_old_output_buffer = @output_buffer;#{locals_code};#{code}
ensure
@virtual_path, @output_buffer = _old_virtual_path, _old_output_buffer
end
end_src
# Make sure the source is in the encoding of the returned code
source.force_encoding(code.encoding)
# In case we get back a String from a handler that is not in
# BINARY or the default_internal, encode it to the default_internal
source.encode!
# Now, validate that the source we got back from the template
# handler is valid in the default_internal. This is for handlers
# that handle encoding but screw up
unless source.valid_encoding?
raise WrongEncodingError.new(@source, Encoding.default_internal)
end
mod.module_eval(source, identifier, 0)
ObjectSpace.define_finalizer(self, Finalizer[method_name, mod])
end
我加入的代码是:
code.insert(63, "@output_buffer.safe_append='\n<!--#{self.inspect} start -->\n'.freeze\;")
code.insert(code.size-19, "@output_buffer.safe_append='\n<!--#{self.inspect} end -->\n'.freeze\;")
它将在视图中的所有谐音,所有的观点的输出,因为他们正在渲染。
结果会是这样:
<!--app/views/profiles/_header.html.erb start -->
Your text on the page
<!--app/views/profiles/_header.html.erb end -->
如果你想提取到一个单独的类,在config/initializers/
目录中的Rails项目,叫做创建一个新的初始化,例如render_override.rb
和粘贴代码的东西像这样:
ActionView::Template.class_eval do
# @override
def compile(mod)
encode!
method_name = self.method_name
code = @handler.call(self)
# This is the code I have added
code.insert(63, "@output_buffer.safe_append='\n<!--#{self.inspect} start -->\n'.freeze\;")
code.insert(code.size-19, "@output_buffer.safe_append='\n<!--#{self.inspect} end -->\n'.freeze\;")
# Make sure that the resulting String to be eval'd is in the
# encoding of the code
source = <<-end_src
def #{method_name}(local_assigns, output_buffer)
_old_virtual_path, @virtual_path = @virtual_path, #{@virtual_path.inspect};_old_output_buffer = @output_buffer;#{locals_code};#{code}
ensure
@virtual_path, @output_buffer = _old_virtual_path, _old_output_buffer
end
end_src
# Make sure the source is in the encoding of the returned code
source.force_encoding(code.encoding)
# In case we get back a String from a handler that is not in
# BINARY or the default_internal, encode it to the default_internal
source.encode!
# Now, validate that the source we got back from the template
# handler is valid in the default_internal. This is for handlers
# that handle encoding but screw up
unless source.valid_encoding?
raise WrongEncodingError.new(@source, Encoding.default_internal)
end
mod.module_eval(source, identifier, 0)
ObjectSpace.define_finalizer(self, Finalizer[method_name, mod])
end
end
重新启动服务器,这样的变化可以捡起来,和你的观点现在将这些新设置渲染。
你可能想用一个检查RAILS_ENV='dev'
或类似的东西来包围你的eval类,这样它可以在开发中运行,并且你准备好了。
让我看看我是否明白这一点。您希望在呈现网页的html源代码中添加注释,其中包含指向您呈现的html的路由,每次都有一个要呈现的调用?这只会在你的开发环境中? – mlabarca
如果我在每次渲染之前获得所需的要求,可以在文件路径中添加注释 –
它不是基于配置的,但可以循环浏览app/views/*并在[shell脚本](http ://stackoverflow.com/questions/10587615/unix-command-to-prepend-text-to-a-file)。 – anyarms