views:

413

answers:

4

there is a line displayed within <pre>

<%= h @stories.inspect %>

and the output was too long, so i changed it to

<%= #h @stories.inspect %>

<% @stories.each do |s| %>
  <%= h s.inspect %>
<% end %>

(commenting out the first line). now the code will fail to compile... saying

compile error
/Users/winterheat/ror/shov2/app/views/stories/index.html.erb:13: syntax error, unexpected kENSURE, expecting ')'
/Users/winterheat/ror/shov2/app/views/stories/index.html.erb:15: syntax error, unexpected kEND, expecting ')'

and if i remove that commented line altogether, the code will work. i thought in some book, it is said that you can comment out some code in ERB like that?

Update: funny if i change it to

<% #h @stories.inspect %>

then it will compile fine... so the displaying of result tag <%= %> doesn't like comments, it seems.

+3  A: 

<%= should be followed by a Ruby expression and is replaced with result.

no expression leads to an error

yawniek
+6  A: 

Think of <%= as meaning "add the value of this expression to the output stream". No expression? Syntax error.

Consider

output << @stories.inspect

vs

output <<

Tracking down how <%= is really handled in the erb source may be edifying. I found it worth the effort when I was getting started with Rails.

Dave W. Smith
but if puts "hello" and puts can both be handled without error, why not <%= # comment %> ?
動靜能量
Because that's an implementation detail specific to puts and has nothing to do with ERB?
Chuck
+1  A: 

use

<% #h @stories.inspect %>

instead of

<%= #h @stories.inspect %>

because

<%= (I expect something that I can convert to string) %>
cocoafan
+2  A: 

The proper way to comment a <%= %> tag in Ruby/Rails is <%#= %> ... place the comment sign before the equals sign. Then everything will work like a charm.

salt.racer