I'm just getting started with Ruby and Ruby on Rails, so excuse me if this is a simple question. I've noticed that in some ERB files, there is a difference to using <%= %> and <% %>, but what is the difference?
Thanks!
The difference is as follows:-
<%= %> would execute and print the value of rails code written inside and <% %> would just execute the rails code.
Thanks, Anubhaw
<%= %> in ERB equals to <?php echo ?> in PHP.
Related
I have a ruby on rails html.erb file with inline code in it like below. However, the code doesn't always run when somebody loads the page. It only prints to stdout the first time the page is loaded.
<%puts "TESTING"%>
Does rails cache my html.erb pages somehow? Is there a way to turn it off?
If you want to output something you need to use <%= %> instead of <% %>
<%= "TESTING" %>
<%= %> - Evaluate and print the output
<% %> - only evaluate
Try below code:
<%= "TESTING" %>
<%= %> is used to print the text in html.erb file
This question already has answers here:
What is the difference between <%, <%=, <%# and -%> in ERB in Rails?
(7 answers)
Closed 7 years ago.
Could someone explain the difference between using <%= some_ruby_code %> and <% some_other_ruby_code %>
I'm aware that they're not equivalent but can't find a good explanation as to why.
Thanks in advance.
In ERB (Embedded Ruby), using <%= %> will both evaluate and render the evaluated Ruby within your markup, whereas just using <% %> will only evaluate the enclosed expression, and will not print anything to the erb file.
Hope it helps!
In other words, with <%= %> you execute ruby code and print the output. Using <% %> is only necessary to run the ruby code.
To clarify, this is Emebedded Ruby which you use for views pages on Rails. This might helpful for you: https://docs.puppetlabs.com/puppet/latest/reference/lang_template_erb.html
This question already has an answer here:
embedded ruby syntax "<%= .. %>" in rails [duplicate]
(1 answer)
Closed 8 years ago.
I've been following Agile Web Development with Rails, and their code suddenly switches from the use of <%= %> to <% %>.
Code Example 1:
<%= 1+2 %>
Code Example 2:
<% for file in #files %>
file name is: <%= file %>
<% end %>
My question is, for the second code sample why can I not use <%= %>?
Thanks!
In ERB (embedded ruby) syntax, <%= %> is shorthand for "perform the following ruby code AND THEN print the result". So in the first example, it will print the result of the operation 1 + 2, 3.
The second example shows a for loop which will iterate and print the contents between the for declaration and its corresponding <% end %>.
To answer your question, the expression for file in #files itself doesn't return anything worth printing so there's no need to use <%= %> and in fact doing so can cause a hard-to-track-down bug.
I'm new to slim and there are little things that I don't understand and I don't find answers in the documentation.
linebreak - How can I add this at the end of a line? For example:
<%= name %><br/>
<%= address %><br/>
How can I combine pure html and ruby on the same line? For example:
<p>New building <% if building.ownver %> for <%= owner %><% end %></p>
I know, I must have missed something but there is no real tutorial out there.
BTW, There is no emulator to convert erb to slim?
Thanks.
You can use this converter html to slim here's a link!
The documentation covers this here: https://github.com/slim-template/slim#inline-html--html-style
The example it gives is:
<html>
head
title Example
<body>
- if articles.empty?
- else
table
- articles.each do |a|
<tr><td>#{a.name}</td><td>#{a.description}</td></tr>
</body>
</html>
I don't know if there's an equivalent to the erb <%= "for #{owner}" if building.ownver %> in slim, so I would just use the above information for your second question, as well, by changing the code to:
- if building.ownver
<p>New building</p>
- else
<p>New building for #{owner}</p>
I have the following code:
<% slika = Refinery::Page.find('sladoledi') %>
<%= link_to (image_tag slika.key_image.url, slika) %>
The problem is that it's not linking to slika. Any suggestions?
Try this format
<%= link_to(slika) do %>
<%= image_tag(slika.key_image.url)%>
<% end %>
also have a look at documentation there are nice examples how to use link_to() helper
http://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to
First start nesting code you write here. People have problem reading code like this :(
If you want add code click code button on editor.
I suppose the problem is you don't end image_tag.
Correct form is:
link_to(image_tag(slika.key_image.url),slika)