Ruby on Rails syntax error, unexpected ‘)’, expecting keyword_then or ‘;’

[Updated July 8, 2011]

It’s a beautiful day here in the San Francisco Bay Area. The sun is shining, not a cloud in the sky. It’s also colder than a well digger’s hooha. After yesterday’s stupendous gale, the north wind is howling back with a vengeance.

Brrr…

So, it’s a perfect day for getting out and about, or for banging out some Rails code. Either works for me!

What doesn’t work for me is banging out some (supposedly) simple code, and getting syntax errors first thing in the morning:

syntax error, unexpected ')', expecting keyword_then or ';'

That’s just bogus.

Here’s the situation:

I have members, and each member has a profile, and I want each member to be able to edit their own profile, but not anyone else’s profile. Thus, I have to ensure the correct member is logged in. Here’s the first cut at the code, where I’ve pushed the view into into a helper _profile_links.html.erb:

<%= if member_signed_in? %>
  <%= link_to 'Edit', edit_profile_path(@profile) %> | 
  <%= link_to 'Delete', profile_path(@profile), :confirm => 'Are you sure?', :method => :delete %> |
<% end %>

This is all well and nice. Devise provides the handy user_signed_in? for us. So why does this result in a nasty error?

/Users/dave/src/project/app/views/profiles/_profile_links.html.erb:3: syntax error, unexpected ')', expecting keyword_then or ';' or '\n'
...ppend= ( if member_signed_in? );@output_buffer.safe_concat('
...                               ^

/Users/dave/src/project/app/views/profiles/_profile_links.html.erb:8: syntax error, unexpected keyword_end, expecting ')'
'); end 
       ^
/Users/dave/src/project/app/views/profiles/_profile_links.html.erb:10: syntax error, unexpected keyword_ensure, expecting ')'
/Users/dave/src/project/app/views/profiles/_profile_links.html.erb:12: syntax error, unexpected keyword_end, expecting ')'

WTH?

Experienced Rails programmers will know instantly what’s going here. In fact, experienced Rails programmers will likely have caught the error before reading past line 1.

Because…

…this isn’t really a syntax error in Rails; it’s an ERb syntax error.

Here’s the correct syntax, can you spot the problem?

<% if member_signed_in? %>
  <%= link_to 'Edit', edit_profile_path(@profile) %> | 
  <%= link_to 'Delete', profile_path(@profile), :confirm => 'Are you sure?', :method => :delete %> |
<% end %>

If you need more help, or you want to understand what’s going on, read the ERb documentation.

Now, the next time I return to Rails after some time off, and I get this error, and search for it, I’ll find my little article here on Riding Rails.

Or, I might find this article on simplifying block helpers while looking for something else.

What about you? What’s your story? How did you get here and did this help you out?

This entry was posted in rails and tagged , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

3 Comments

  1. Posted April 18, 2012 at 8:49 pm | Permalink

    Thanks for this! I just ran into the same problem & your blog was first in Google.

  2. Fritz
    Posted May 3, 2012 at 11:38 am | Permalink

    Thanks so much for this, it was driving me crazy!!

  3. Posted July 27, 2013 at 6:14 pm | Permalink

    Thank you so much! Was in the midst of making a simple Sinatra app and this error halted all progress.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


dool.in