Differencebetween <%= ... %> and <% ... %> in Ruby on Rails

What is the difference between %= ... % and % ... % in Ruby on Rails

This is ERB templating markup (one of many templating languages supported by Rails). This markup:

<% ... %>

is used to evaluate a Ruby expression. Nothing is done with the result of that expression, however. By contrast, the markup:

<%= ... %>

does the same thing (runs whatever Ruby code is inside there) but it calls to_s on the result and replaces the markup with the resulting string.

In short:

<% just run code %>
<%= run code and output the result %>

For example:

<% unless @items.empty? %>
<ul>
<% @items.each do |item| %>
<li><%= item.name %></li>
<% end %>
</ul>
<% end %>

In contrast, here's some Haml markup equivalent to the above:

- unless @items.empty?
%ul
- @items.each do |item|
%li= item.name

Difference between % ... % and %= .. % in rails 3

From the Rails docs, concat is only supposed to be used within a <% %> code block. When you use it in a <%= %> code block, you see it twice because concat appends the provided text to the output buffer, but then it also returns the entire output buffer back to your helper method, which is then output by the <%=, causing your entire page to be duplicated.

Normally, you should not need to use concat much if at all (I've never come across a situation where I needed it). In your helper, you can just do this:

def test
"Hello world"
end

And then use <%= test %> in your view.

What is the difference between %, %=, %# and -% in ERB in Rails?

<% %>

Executes the ruby code within the brackets.

<%= %>

Prints something into erb file.

<%== %>

Equivalent to <%= raw %>. Prints something verbatim (i.e. w/o escaping) into erb file. (Taken from Ruby on Rails Guides.)

<% -%>

Avoids line break after expression.

<%# %>

Comments out code within brackets; not sent to client (as opposed to HTML comments).

Visit Ruby Doc for more infos about ERB.

Difference between -% and % in rails

The extra dash makes ERB not output the newline after the closing tag. There's no difference in your example, but if you have something like this:

<div>
<% if true -%>
Hi
<% end -%>
</div>

It'll produce:

<div>
Hi
</div>

and not this:

<div>

Hi

</div>

Difference between % % and %= % in RoR

Think of it like this:

<% execute this code and display nothing %> 

and

<%= execute this code and display the result in the view %> 

So, for example you could do this:

<% @values = ['eenie', 'menie', 'miney', 'mo' ] %>
<% @values.each do |value| %>
The current value is <%= value %>!
<% end %>

Ruby on Rails Syntax % vs %=

<% %> and <%= %> both execute Ruby code.

<% %> will execute Ruby code, but will not render the return value into html.
<%= %> will execute Ruby code, and will render the return value into html.

What is the difference between :to and = in rails

In context of Rails routes:

  • Is there a difference between these two statements?

There is no difference.

  • If so why is one better than the other?

No, it's the same.

  • Why is the rails community switching to the ":" notation (or are
    they)?

Just a more readable, 'from' => 'to' and 'from', to: 'to'

  • Moving forward with rails 4 and soon 5, are both formats still
    acceptable?

Yes.

The => notation it's a hash ruby feature, and related to the :symbol.
You can write symbols by two ways :key => value and key: value.

The difference between %=, % and %- in ejs

The following is from ejs docs (tag section):

  • <% 'Scriptlet' tag, for control-flow, no output
  • <%= Outputs the value into the template (HTML escaped)
  • <%- Outputs the unescaped value into the template

See the difference between escaped and unescaped html here

in rails difference between form_for @article and form_for :article

form_for(@article) creates a form builder which is bound to a model instance.

If @article is nil it will raise an error.

If the instance is a new record the form will use method="POST" and action="/arcticles".

If the record has been persisted it will have method="PATCH" and action="/arcticles/:article_id".

Rails derives the URL for the action attribute based on convention over configuration. So there is no need to explicitly pass the url option if you follow the conventions.

An example of this would be:

<% @article = Article.new(title: 'Hello World') %>
<%= form_for(@article) do |f| %>
<%= f.text_input :title %>
<% end %>

This will render something like:

<form action="/articles" method="POST">
<input type="text" name="article[title]" value="Hello World"/>
...
</form>

<%= form_for @article, url:{action: "update"} do |form| %> this one
works, but i dont understand how come the submit button says 'update
article'

The form builder knows it is updating an record by calling .new_record? on the the record you passed to form_with. You can change the default value of the submit button by providing translations:

# config/locales/en.yml
en:
helpers:
submit:
create: "Save new record"
update: "Save changes"

form_for(:article) creates a scoped form builder that does not wrap an object.

This creates a form builder where the inputs will be "scoped". For example:

<%= form_for(:article) do |f| %>
<%= f.text_input :title %>
<% end %>

This will render something like:

<form action="/articles" method="POST">
<input type="text" name="article[title]"/>
...
</form>

Rails derives the URL for the action attribute based on convention over configuration.

In your case <%= form_for :article, url:{action: "update"} do |form| %> causes a routing error since form_for defaults to method: "POST".

form_with is the Rails 5.1 replacement for form_for and form_tag

form_with will replace the form_for and form_tag methods which are closely related yet have very different signatures. form_for and form_tag have been soft depreciated and are slated for removal.

The idea is to provide a single method with a more consistent signature.

If you are using Rails 5.1+ this is what you should be using.

See:

  • Rails Guides - Action View Form Helpers
  • Rails API - ActionView::Helpers::FormHelper
  • Rails 5.1's form_with vs. form_tag vs. form_for


Related Topics



Leave a reply



Submit