开发者

nested content_tags only showing the last of the chilldren rails view helper

开发者 https://www.devze.com 2023-04-09 21:20 出处:网络
I am trying to do a little reusable helper to insert into a page of some descriptions with the content tag

I am trying to do a little reusable helper to insert into a page of some descriptions with the content tag

def spec_description(name, overview, detail)
  content_tag :dl do
    content_tag :dt do
      content_tag(:strong, name)
    end
    content_tag :dd, overview, :class => "sp开发者_运维技巧ec-overview"
    content_tag :dd, detail,   :class => "spec-detail" #only this dd tag gets output
  end
end

But as it is, only the dd tag with what is to be 'detail' gets output to the html

UPDATED output html is like this now:

<dl>
   <dd>some detail from detail variable</dd>
</dl>

See how the "overview" and "name" dd tags are completely missing? Let alone their content...

Does anyone have an idea why this is and how I may fix it?


Your helper is returning some HTML and its return value is whatever content_tag :dl returns. The content of the <dl> will be whatever its block returns and the block returns the last value (i.e. the last <dd>). So you just have a return value problem:

def spec_description(name, overview, detail)
  content_tag :dl do
    html  = content_tag :dt { content_tag(:strong, name) }
    html += content_tag :dd, overview, :class => "spec-overview"
    html += content_tag :dd, detail,   :class => "spec-detail"
    html
  end
end
0

精彩评论

暂无评论...
验证码 换一张
取 消