将内容标记嵌套在rails中

嘿。 我有一些来自Agile Web Development的代码,它围绕方法调用包装了一些HTML,如下所示:

# from tagged_builder.rb, included as a FormBuilder helper def self.create_tagged_field(method_name) define_method(method_name) do |label, *args| @template.content_tag("p", @template.content_tag("label" , label.to_s.humanize.capitalize, :for => "#{@object_name}_#{label}") + super) end end 

我想在标签content_tag中嵌套span标签,这样最终的输出将是:

 

我想知道如何包含跨度的内容(比如’警告’等变量)

我尝试了各种各样,但都没有用。 方法调用ok(例如f.text_field:name将生成

 

试过这个:

  def self.create_tagged_field(method_name) define_method(method_name) do |label, warning, *args| @template.content_tag("p", @template.content_tag("label" , label.to_s.humanize.capitalize+ content_tag("span", warning), :for => "#{@object_name}_#{label}") + super) end end 

但没有运气。 任何人都可以引导我朝着正确的方向前进吗? 谢谢

你需要调用@template.content_tag 。 你在那里的代码只是调用self.content_tag,它显然没有做任何事情。

只是想发布最终的解决方案,更多的是为了骄傲而不是其他任何东西。 Noob ……:0

  def self.create_tagged_field(method_name) define_method(method_name) do |label, *args| # accepts the warning hash from text_field helper if (args.first.instance_of? Hash) && (args.first.keys.include? :warning) warning = args.first[:warning] end @template.content_tag("label" , label.to_s.humanize+(@template.content_tag("span", warning, :class =>'small')), :for => "#{@object_name}_#{label}") + super end end