Ok looking over some podcasts I've seen that in Rails 3, we need to use the <%= %>
tag now when we didn't before. However just putting this in my application.html.erb file:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head></head>
<body>
<%= if true %>
true
<% end %>
</body>
</html>
does not render 'true' as I expected. Instead I get an error:
Showing /app/views/layouts/application.html.erb where line #5 raised:
/app/views/layouts/application.html.erb:5: syntax error, unexpected ')', expecting keyword_then or ';' or '\n'
');@output_buffer.append= ( if true );@output_buffer.safe_concat('
^
/app/views/layouts/application.html.erb:7: syntax error, unexpected keyword_end, expecting ')'
'); end
^
/app/views/layouts/application.html.erb:10: syntax error, unexpected keyword_ensure, expecting ')'
/app/views/layouts/application.html.erb:12: syntax error, unexpected keyword_end, expecting ')'
Extracted source (around line #5):
2: <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
3: <head></head>
4: <body>
5: <%= if true %>
6: true
7: <% end %>
8: </body>
going back to the old <% %>
tag fixes this problem. Is this correct? Did the rails developers decide to switch back to the old tag or am I missing something?
rails -v
gets me: Rails 3.0.5
Any ideas?
This only applies to some tags. E.g. <% form_for ... %>
became <%= form_for ... %>
. In this case you just use <% if true %>
. The reason is that form_for
generates output, so you use <%= %>
. A simple if statement does not generate output, so you use <% %>
. The same goes for loops etc.: you just keep using <% %>
.
It is explained here too:
form_for will insert form tags into the view around the content in the block, but there is no equals sign in the erb tags. This breaks the rule that erb blocks that output code to the view should use <%= %> and has made this difficult to work with the internals of form_for in previous versions of Rails. From [Rails version 3] onwards, however, we use an equals signs here as we would with any other erb code that generates output.