2

I'm trying to create something similar to this: Using helpers in rails 3 to output html

Here's my helper:

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

But it's returning all sorts of syntax errors:

/~/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

What am I doing wrong here? Maybe using partial would be better.

1 Answer 1

3

The main problem is that you have spaces between content_tag and (. Try this:

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
Sign up to request clarification or add additional context in comments.

5 Comments

This works, thanks. I thought you had to parse hashes to content_tag?
It wasn't because of the hash notation. I fixed some spacing issues and just rewrote the code how I would. Happy to help.
oh I didn't know you could write it like that.
I does still need the spacing removed from in between the content_tag & the brackets - as well as src changed to href besides that works a treat. Thanks
I had the same problem and your example helped me but it didnt work untill I added "concat" before the content_tag with a "li" element

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.