rails ERB template language construct for <%= vs <% compared with django templates - ruby-on-rails

coming from Django I am wondering how rails people think about <%= %> vs <% %>
From what I see here <%= tag.title %> displays information that's already present like attributes of the obj, like the {{ }} in django, and <% %> always does stuff, like an each loop or if statement, like the {% %} in django.
If that statement fully accurate, or is there a finer line I missed? Thanks

Yeah you've basically got it down.
<% %>
Will run ruby code without displaying it
<%= %>
Will display the information to the screen
<%# %>
Will comment out ruby code in your view
Here's just a simple example
<% #users.each do |user| %>
<%= user.email %>
<% end %>
This question may help you as well.

Related

Ruby on rails - each do class?

<% consents_checkboxes.each do |checkbox| %>
<%= checkbox.html_safe %>
<% end %>
Hello there,
can i give them a class while looping through them? I can't get it to work and tried several different ways.
This is something I would like to achieve
<% consents_checkboxes.each do |checkbox| %>
<%= checkbox.html_safe, class: 'checkbox' %>
<% end %>
thank you
You can only do it with an element. What you want to do is:
<% consents_checkboxes.each do |checkbox| %>
<p class="checkbox"><%= checkbox.html_safe %></p>
<% end %>
Of course, you can use another element (span, div etc.).
What's on consents_checkboxes? You should provide more context when you ask for something...
It looks like you have strings with the html code, right? you will have to parse the string with something like nokogiri and add a class
<%= Nokogiri.parse(checkbox).add_class('checkbox') -%>
Or you could modify the process that generates that consents_checkboxes to include the class you need. Maybe there's better options, but with only that information it's really hard to tell.

Ruby on Rails - Underline words if they appear in dynamically generated text using Ruby

I am trying to underline words that are dynamically generated by the debug(params) method provided by rails. I have something below, but it obviously does not work, plus what I have below is attempt to try and change the words using methods that I already know about (like the .upcase method). I was hoping to underline the word controller if it appears in the text using only Ruby. Can anyone help me out here?
<%= debug(params) if Rails.env.development? %>
<% if debug(params).include?('controller:') %>
<%= 'controller'.upcase %>
<% end %>
thanks
edit:
I should add that debug(params) is a method defined by RAILS, I was able to do the following which seems even more off, so far the answers have not been correct to what I want to do.
<% if Rails.env.development? %>
<% debug_method = debug(params).split.each do |word| %>
<% if word == 'controller:' %>
<ul><% word.upcase %></ul>
<% end %>
<% end %>
<%= debug_method.join %>
<% end %>
which returns the following text: https://ibb.co/cvnEpw , keep the answers coming in though. I want to get the words in the original box (that's generated by the method to underline the controller word https://ibb.co/jmSm2G).
use <u></u> tag
<%= debug(params) if Rails.env.development? %>
<% if debug(params).include?('controller:') %>
<u><%= 'controller'.upcase %></u>
<% end %>
example here
Provide the css to generate html element:
p { text-decoration: underline; }
Add html elemnt to wrap your words:
<%= debug(params) if Rails.env.development? %>
<% if debug(params).include?('controller:') %>
<p> <%= 'controller'.upcase %> </p>
<% end %>
The answer to the question is below. I had to use the .gsub and .html_safe methods.
<%= debug(params).gsub("controller:", "<u>controller:</u>").html_safe %>
This code keeps the existing html & css generated by rails intact

Rendering issue with <dc:creator> tag from Wordpress RSS2

I'm having issues with rendering the content of the XML tag from my Wordpress RSS feed. Here's my code to display the last 2 posts which I draw in from the controller:
<h3>Check out our latest blog posts:</h3>
<% unless #latest_blog_posts.nil? %>
<% #latest_blog_posts.each do |post| %>
<% if nil != post && post.respond_to?(:pubDate) %>
<h4><%= link_to post.title, post.link, :target => "_blank" %>
(by: <%= post.dc:creator %> - <%= time_ago_in_words(post.pubDate) %> ago)</h4>
<%= (post.description).slice!(0, 195).html_safe %>[...]
<% end %>
<% end %>
<% else %>
<p>Woops, looks like there's no posts to show. Sorry about that.</p>
<% end %>
The tag gives an error due to the ":" in the tag. I've tried using another variable and rendering the contains in a string:
article_author = '#{post.dc:creator}'
That renders "#{post.dc:creator}" in the view (I thought it would but I gave it a try anyway). Does anyone have a solution to this? Thanks.
Might as well answer my own question since it's the first result in a relative Google search. Hopefully it can help someone out in the future. The xml node was being parsed as:
dc_creator
<%= post.dc_creator %>
Thanks to everyone that checked out my question and tried to help.

How to obtain the same result as <%= var %> without the =?

In the Rails views, I regularly find lines like:
<%= my_var %>
What if I had a slightly more complex situation and I needed to trigger the printing with plain code instead of <%= %>?
Example:
<% .....
puts my_var
%>
I guess is a silly question but bear with me, I'm a ruby beginner.
Look at documentation of ERB
In <% %> you put expressions that are not for printing out.
In <%= %> you put code for printing out.
Example:
<% if #cost < 10 %>
<b>Only <%= #cost %>!!!</b>
<% else %>
Call for a price, today!
<% end %>
You can use helper method which is much more cleaner.

How do I run multiple lines of Ruby in html.erb file

I'm using Ruby on Rails and need to run a block of Ruby code in one of my html.erb files. Do I do it like this:
<% def name %>
<% name = username %>
<%= name %>
or like this:
<% def name
name = username %>
<%= name %>
Thanks for reading.
If you need extra functions in your view, you normally declare those inside a helper.
For each controller, if there is a helper it is automatically loaded. For instance, if you have a PeopleController, in the app/helpers folder, there should be a people_helper.rb, and it should look like this
module PeopleHelper
def name
#do something
username
end
end
Another, very clean alternative, is to use the Presenter pattern, but i think it is less common (unfortunately).
Otherwise, if you do need multiple lines of ruby code inside a erb view, which i try to avoid, i prefer the following style:
<%
counter_1 = 0
counter_2 = 1
do_some_more_prep_here
%>
<% #records.each do |rec|%>
<%# do something with the prepped date in each row %>
<% end %>
Also for me code indentation is more important than html indentation, so i will prefer something like
<table>
<% #rows.each do |row| %>
<tr>
<td><%= row.item1 %></td>
<% if row.some_test %>
<td><%= row.item2 %></td>
<% end %>
</tr>
<% end %>
</table>
But i am always very interested to hear different opinions in this matter.
It is unusual to define a method in an ERB file, so I recommend against it.
If you want to call a block like #each, you can do something like the following:
<% names.each do |name| %>
<%= name %>
<% end %>
Don't forget the <% end %>.
I can imagine someone needing it in one particular template (no point in creating a helper) to not duplicate html markup. That is, when resulting html page has a couple of similar blocks of html markup. Though, it can easily be abused (unreadable code).
<% def f1(a, b, c) %>
test: <%= a %>, <%= b %>, <%= c %>
<% end %>
<% f1(1, 2, 3) %>
<% f1(4, 5, 6) %>

Resources