Why the use of both <% and <%= in the views?

If I write something like:

<% if signed_in?.blank? %> or <%= link_to "Sign Up", sign_up_path %>

What is the difference between the two signs of <% and <%=?

Why make it this way instead of using just one for simplicity?

When do I know I need to use <% over <%=?

Answers


<%= puts the return value of the code inside to the page.

<% just execute code.

Here is the good guide about ERB http://api.rubyonrails.org/classes/ActionView/Base.html


<% %> Simply executes the statement(s) inside that block, whereas <%= %> will output the result of the statement.

So for example, with the <% if signed_in?.blank? %>, the ruby interpreter just executes that code and checks if signed_in is blank.

The <%= link_to %> statement will actually generate HTML.


Need Your Help

Marshal.PtrToStringUni() vs new String()?

c# .net marshalling unmanaged unsafe

Suppose i have a pointer of type char* to unicode string, and i know the length: