9

i found this "executed with no substitution back into the output" , but maybe my English wasn't too good , i cant really understand what it means. Can anyone help out?

wizztjh
  • 6,979
  • 6
  • 58
  • 92
  • In the Rails context this sort of thing almost always means 'flow control' -- it's code that doesn't return a value, but rather tells other code whether and when to fire. – Joseph Weissman Oct 17 '10 at 07:36

3 Answers3

17

<% %>

Will execute Ruby code with no effect on the html page being rendered. The output will be thrown away.

<%= %>

Will execute Ruby code and insert the output of that code in place of the <%= %>

example...

<% puts "almost" %> nothing to see here 

would render as

nothing to see here

however

<%= puts "almost" %> nothing to see here

would render as

almost nothing to see here
Michelle Six
  • 655
  • 3
  • 13
7

Sometimes you will have to (or you want to) execute some ruby statements but not for output purpose.

like the following:

<% if @user.nil? %>
  Hi, welcome!
<% else %>
  Hi, <%= @user.name %>!
<% end %>

Of course this is just one use case, but sometimes you do need <% %> :D

PeterWong
  • 15,951
  • 9
  • 59
  • 68
1

Code in <% %>(without equal) is executed "with no substitution back into the output" means you want to execute code WITHOUT any output, like a loop and the best part is, it can be used with a non ruby code.

<% 3.times do %>

<h1>Hello world</h1>

<%end%>

This will give:

<h1>Hello world</h1>  
<h1>Hello world</h1>  
<h1>Hello world</h1>  
zengr
  • 38,346
  • 37
  • 130
  • 192