1

I have a tiny haml file that's causing all sorts of irrational error messages. The file is this:

%h1 Collaborator List for Private Wiki "#{@wiki.title}"
- for @wiki.relationships.each do |r|
  - if !r.created_creator do
    %h3 Users.email.where(relationship=r)

The error messages are:

/home/vagrant/code/blocipedia/app/views/wikis/collaborator_list.html.haml:4: syntax error, unexpected keyword_ensure, expecting keyword_end ...:Util.html_safe(_erbout);ensure;@haml_buffer = @haml_buffer.... ... ^ /home/vagrant/code/blocipedia/app/views/wikis/collaborator_list.html.haml:4: syntax error, unexpected ';', expecting :: or '[' or '.' /home/vagrant/code/blocipedia/app/views/wikis/collaborator_list.html.haml:7: syntax error, unexpected end-of-input, expecting keyword_end

I imagine the problem has to do with nesting, but I can't see it. Can anyone help me out?

Ellen W
  • 217
  • 4
  • 16
  • Completely randomly, you should consider using select or reject here: #wiki.relationships.select { |r| !r.created_creator }.each or #wiki.relationships.reject { |r| r.created_creator }.each to eliminate the if statement. (Forgive the #s, the validator doesn't like the at-signs.) – John LeBoeuf-Little Nov 20 '14 at 16:05
  • Thanks! I have several places where I can use this advice. – Ellen W Nov 21 '14 at 01:14

1 Answers1

4

That error message typically means that you've missed a do clause for an iterator. In your case, your Ruby clauses in the HAML are invalid, specifically using both a for and an enumerator (#each), and do following the if.

%h1 Collaborator List for Private Wiki "#{@wiki.title}"
- @wiki.relationships.each do |r|
  - if !r.created_creator
    %h3 Users.email.where(relationship=r)

You can make this a bit nicer, though:

%h1 Collaborator List for Private Wiki "#{@wiki.title}"
- @wiki.relationships.reject(&:created_creator).each do |r|
  %h3 Users.email.where(relationship=r)

This first rejects all entries from the relationships where r.created_creator evaluates to a truthy value, then iterates over the remaining values, giving you both your selection criteria and your iteration.

Chris Heald
  • 61,439
  • 10
  • 123
  • 137