Ellen W
Ellen W

Reputation: 227

haml error messages: expecting keyword_end, etc

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?

Upvotes: 1

Views: 990

Answers (1)

Chris Heald
Chris Heald

Reputation: 62638

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.

Upvotes: 4

Related Questions