我正在尝试创建与此类似的内容: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会更好。
答案 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