Rails嵌套列表,其中包含Helper中的concat content_tag

时间:2013-04-01 12:37:10

标签: html ruby-on-rails helper concat

我正在尝试创建与此类似的内容:Using helpers in rails 3 to output html

这是我的帮手:

def section_references_to_html (section)
     concat content_tag (:ol, :class => 'sources') do
        section.references.each do |reference|
          concat content_tag (:li,content_tag(:a, reference.text, :src => reference.url))
        end
      end
  end

但它会返回各种语法错误:

/~/app/helpers/application_helper.rb:21: syntax error, unexpected ',', expecting ')'
     concat content_tag (:ol, :class => 'sources') do
                             ^
/~/app/helpers/application_helper.rb:21: syntax error, unexpected ')', expecting keyword_end
     concat content_tag (:ol, :class => 'sources') do
                                                  ^
/~/app/helpers/application_helper.rb:22: syntax error, unexpected keyword_do_block, expecting keyword_end
        section.references.each do |reference|
                                  ^
/~/app/helpers/application_helper.rb:23: syntax error, unexpected tIDENTIFIER, expecting keyword_do or '{' or '('
          concat content_tag (:li,content_tag(:a, referen...
                            ^
/~/app/helpers/application_helper.rb:23: syntax error, unexpected ',', expecting ')'
...       concat content_tag (:li,content_tag(:a, reference.tex...
...                               ^
/~/app/helpers/application_helper.rb:23: syntax error, unexpected ')', expecting keyword_end

我在这里做错了什么?也许使用partial会更好。

1 个答案:

答案 0 :(得分:3)

主要问题是content_tag(之间有空格。试试这个:

def section_references_to_html(section)
  content_tag(:ol, class: 'sources') do
    section.references.each do |reference|
      content_tag(:li, content_tag(:a, reference.text, href: reference.url))
    end
  end
end