Confusion over ERB delimiters in Rails form partial

9 views
Skip to first unread message

gvim

unread,
Apr 28, 2016, 11:29:16 AM4/28/16
to rubyonra...@googlegroups.com
"Agile Web Development with Rails 5", p.76 lists a form partial which
begins:

<%= form_for(product) do |f| %>
<% if product.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(product.errors.count, "error") %> prohibited
this product from being saved:</h2>

<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

Colin Law

unread,
Apr 28, 2016, 11:37:39 AM4/28/16
to Ruby on Rails: Talk
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

gvim

unread,
Apr 28, 2016, 12:17:26 PM4/28/16
to rubyonra...@googlegroups.com
Sorry, should have RTFM first :)

gvim
Reply all
Reply to author
Forward
0 new messages