LearningRoR
LearningRoR

Reputation: 27212

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 <%=?

Upvotes: 5

Views: 95

Answers (2)

Zajn
Zajn

Reputation: 4088

<% %> 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.

Upvotes: 1

Flexoid
Flexoid

Reputation: 4245

<%= 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

Upvotes: 10

Related Questions