Why the use of both <% and <%= in the views? - ruby-on-rails

If I write something like:
<% if signed_in?.blank? %> or <%= link_to "Sign Up", sign_up_path %>
What is the difference between the two signs of <% and <%=?
Why make it this way instead of using just one for simplicity?
When do I know I need to use <% over <%=?

<%= puts the return value of the code inside to the page.
<% just execute code.
Here is the good guide about ERB http://api.rubyonrails.org/classes/ActionView/Base.html

<% %> Simply executes the statement(s) inside that block, whereas <%= %> will output the result of the statement.
So for example, with the <% if signed_in?.blank? %>, the ruby interpreter just executes that code and checks if signed_in is blank.
The <%= link_to %> statement will actually generate HTML.

Related

rails4 behavior based on link_to is nil

There is a website attr on product_lead table which is optional. If it's present then I wanna turn #produc_lead.lead into a link, but if it's not it should be plain text.
If I use the code below and the website is nil then the link points to the page the user is currently on. If I do it with #product_lead.try(:website), it's gonna be the same. But as I mentioned I would like to have plain text over link in this case.
<%= link_to #product_lead.website, target: "_blank" do %>
<%= #product_lead.lead %>
<% end %>
After playing around I fell back to the following solution, but it's terrible. Any better ideas?
<% if #product_lead.website %>
<%= link_to #product_lead.website, target: "_blank" do %>
<%= #product_lead.lead %>
<% end %>
<% else %>
<%= #product_lead.lead %>
<% end %>
Maybe link_to_if if Rails 4
<%= link_to_if(#product_lead.website, #product_lead.lead, #product_lead.website) do %>
#product_lead.lead
<%= end %>
You can create custom view helper for this.
Well, link_to is going to generate a <a> tag, whether you provide a valid URL or not. So if the URL is nil, yes, it's gonna be a link for you own page.
If you want to "hide" this, you could call a partial in which you place you if/else and so on, but it's just to sweep this under the rug :)
Or if you wanna go further, as #Jovica Šuša, a view helper is the most elegant solution.

Comments from post not displaying correctly in rails

Been trying to set up a regular blog in Ruby on Rails, and finally got the comment system to work within a post. However, when I loop through each comment and try to output the comment's title it displays this,
awef [#<Comment id: 6, title: "awef", link: nil, campaign_id: 5, user_id: 1, created_at: "2015-09-24 09:46:43", updated_at: "2015-09-24 09:46:43">]
Instead of just the title. How can I fix this?
The loop in your view should be something like this:
<% #blog.comments.each do |f| %>
<%= f.title %>
<% end %>
Please check you are using the right angular parenthesis (<%= %>) and not placing p puts or inspect commands inside them.
Edit:
Now that you show use the code, the problem is in the first angular parenthesis: should be <% not <%=. The first is for the logic, the latter to output erb code.
<% #blog.comments.each do |f| %> # remove "="
<%= f.title %>
<% end %>
<% ... %>: Executes the ruby code within the brackets
<%= ...%>: Executes the ruby code and show the executing result in webpage
Classic mistake, it's because you're using <%= ERB output tags for the .each loop. You can view more information on how to write correct ERB here.
You need to replace your <%= output tags with standard ERB tags <%:
<% #blog.comments.each do |f| %>
I was stung by that one myself when I was starting out.
The problem you have is probably the = in your each statement.
Try to use this:
<% #blog.comments.each do |f| %>
<%= f.title %>
<% end %>
Notice the removed = at <% #blog.comments.each do |f| %>
The reason is as the <%= %> always prints while <% %> only executes the code.

What is the difference between <%= and the "puts" command in rails?

I have a segment of code:
<% #public_address.each do |key| %>
<%= key["address"]["address"] %>
<% end %>
This displays the keys properly, but this code
<% #public_address.each do |key| %>
<% puts key["address"]["address"] %>
<% end %>
displays nothing. What gives? What's the difference between the two?
The <% %> and <%= %> are used in erb to execute ruby code when rendering a template.
Erb is the default template engine in rails.
Difference between <% %> and <%= %>
<% %> Will evaluate the ruby code it contains, but "silently".
Meaning that no output is going to be printed on the rendered page.
<%= %> on the other end, evaluates the ruby it contains and
renders the result on the rendered page.
What's the difference between <% code %> and <%= code %> in Rails erb?
What's puts?
Puts is simply a method from Ruby that is used to print a string at runtime. It has nothing to do with erb templates.
In your first bit of code <%= key["address"]["address"] %>, the <%= %> is rails syntax for evaluating the code inside and returning the value.
In your second bit of code <% puts key["address"]["address"] %>, you use <% %>, which doesn't return an evaluated rails statement. Furthermore, puts is a method that outputs whatever follows it to the stout object. In a command line program, that means printing out to the terminal screen, but in a web app you aren't working with a terminal screen. You are working with controllers and view templates, so it is necessary to use the evaluative <%= %> if you want to return values that will be displayed in the view.

Rails instance variable join

This is so simple but it's been ages since I needed this.
I have the following code
<% #miniature.minisets.each do |miniset| %>
<%= link_to miniset.name, miniset %>
<% end %>
It outputs Minisetname Minisetname Minisetname etc
I want it to output Minisetname, Minisetname, Minisetname with commas.
I've tried to include .join(", ") but can't find the right place to put it. Do I also need to use .map instead of .each?
Ignominy.
Here's one way that ought to work:
<%= #miniature.minisets.map { |miniset| link_to miniset.name, miniset }.join(", ").html_safe %>

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.

Resources