将内容标签嵌套在Rails中 [英] Nesting content tags in rails

查看:66
本文介绍了将内容标签嵌套在Rails中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿.我从敏捷Web开发那里得到了一些代码,这些代码将HTML封装成这样:

Hey. I've got some code from Agile Web Development that wraps some HTML around a method call as so:

  # 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标签,以便最终输出如下:

I would like to nest a span tag within the label content_tag, so that the final output would be along the lines of:

<p><label>Name
        <span class="small">Add your name</span>
    </label>
    <input type="text" name="textfield" id="textfield" />

我想知道如何包含跨度的内容(例如警告"之类的变量)

I am wondering how I go about including the span's content (say a variable such as 'warning')

我尝试了各种尝试,但无济于事.调用ok的方法(例如f.text_field:name将产生

I have tried all sorts, to no avail. The methods call ok (such as f.text_field :name will produce

<p><label for="object_name">Name</label></p>

尝试过:

  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

但是没有运气.谁能指引我正确的方向?谢谢,A

But no luck. Can anyone steer me in the right direction? Thanks, A

推荐答案

您需要致电@template.content_tag.您那里的代码只是调用self.content_tag,显然它什么也没做.

You need to call @template.content_tag. The code you have there is just calling self.content_tag, which obviously doesn't do anything.

这篇关于将内容标签嵌套在Rails中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆