Confusion over ERB delimiters in Rails form partial

“Agile Web D. with Rails 5”, p.76 lists a form partial which
begins:

<%= form_for(product) do |f| %>
<% if product.errors.any? %>


<%= pluralize(product.errors.count, “error”) %> prohibited
this product from being saved:

   <ul>
   <% product.errors.full_messages.each do |message| %>
     <li><%= message %></li>

It is correct but I’m confused about the first line as I understood that
control structures begin with <% as in the 2nd line and that <%= is
reserved for inserting values and calling formatting functions.

gvim

On 28 April 2016 at 16:28, gvim [email protected] wrote:

  <% product.errors.full_messages.each do |message| %>
    <li><%= message %></li>

It is correct but I’m confused about the first line as I understood that
control structures begin with <% as in the 2nd line and that <%= is reserved
for inserting values and calling formatting functions.

That is almost correct. Lines with <% are evaluated but nothing is
inserted into the page. Lines with <%= are evaluated and the returned
value (string) is inserted into the page. Since form_for has to
insert the form tag into the page then it has to be <%=

Colin

On 28/04/2016 16:36, Colin L. wrote:

On 28 April 2016 at 16:28, gvim [email protected] wrote:

That is almost correct. Lines with <% are evaluated but nothing is
inserted into the page. Lines with <%= are evaluated and the returned
value (string) is inserted into the page. Since form_for has to
insert the form tag into the page then it has to be <%=

Colin

Sorry, should have RTFM first :slight_smile:

gvim

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs