[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.
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
<%= 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 ')'
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.
…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?