How to add a line break with conditional operators - ruby-on-rails

OK, stupid newbie question: how do you make a line break only when it's needed?
I'm creating a basic address listing and only want to include a line of an address if it isn't blank. How do I keep the blank line from printing? I've tried including the break and new line tags, and tried using puts and quotation marks of both varieties and escaping the slashes but can't seem to display the address correctly.
Is there a way to have each line of the address to print on its own line or simply omit the line if there is no info to put on it?
Here's the current version of the code:
<p><strong>Main Address</strong></p>
<p><%= if #vendor.address1 || null
#vendor.address1 #need a break here
end %>
<%= if #vendor.address2 || null
#vendor.address2 #need a break here
end %>
<%= #vendor.city %>, <%= #vendor.state %> <%= #vendor.zip %></p>

This is how I would do it:
<p>
<strong>Main Address</strong>
</p>
<p>
<% unless #vendor.address1.blank? %>
<%= #vendor.address1 %><br>
<% end %>
<% unless #vendor.address2.blank? %>
<%= #vendor.address2 %><br>
<% end %>
<%= #vendor.city %>, <%= #vendor.state %> <%= #vendor.zip %>
</p>
By the way: the || null in your code is not valid Ruby. null does not exist, it should be nil. But even if you had used nil, your code does not do what you expect it to do. For these kind of things, you'd better use blank?.

<p><strong>Main Address</strong></p>
<p><%= #vendor.address1%><%= <br/> if #vendor.address1.blank? %>
<%= #vendor.address2%><%= <br/> if #vendor.address2.blank? %>
<%= #vendor.city %>, <%= #vendor.state %> <%= #vendor.zip %></p>

Related

Print string while adding newlines

I'm trying to print out a string with embedded ruby to a webpage, if I use the following it ends up going off of the page.
<%= comment.body %>
I want to do something like this so that it automatically prints everything with new lines
<%= comment.body[0..136] %>
<%= "\n" %>
<%= comment.body[137..250] %>
I've done the following however it prints the characters with spaces between them and ignores spaces in the string.
<% for i in 0..comment.body.length do %>
<%= i %>
<% end %>
Thanks
i'm not sure but it can help you some how
<%=simple_format(comment.body, html_options={ class: 'your_class'}, options={})%>
for more details and modification follow this
Why not use HTML?
<% for i in 0..comment.body.length do %>
<%= i %><br>
<% end %>
Solved it by using CSS
in HTML:
<p id="commentBody">
...
</p>
in CSS:
#commentBody {
word-wrap: break-word;
}

checkboxes for all elements in a model

I want to display a checkbox for all elements in a model called MyModel. Here is what I wrote:
<%= MyModel.all.each do |c| %>
<%= check_box_tag(:id) %>
<%= label_tag(:name, c[:name]) %><br>
<% end %>
It does show the checkboxes as expected but at the end, I also get a list of the model content as shown in this screenshot.
Actually, it seems to be related to <%= MyModel.all.each do |c| %> because just printing out simple text still prints the whole model table content at the end:
<%= MyModel.all.each do |c| %>
toto<br>
<% end %>
shows this screenshot
Any idea how to get rid of this list at the end?
Thanks!
<%- MyModel.all.each do |c| %>
<%= check_box_tag(:id) %>
<%= label_tag(:name, c[:name]) %><br>
<% end %>
- - evaluates code.
= - evaluates code and outputs.

Conditional line spacing in ERb partials

This is the code for an address partial I just wrote. People might put single line addresses in either street line, company name is optional, etc... It works exactly how I want it to, but I know that checking each variable twice is ugly and terrible.
<%= "#{a.name}" unless a.name.blank? %>
<% unless a.name.blank? %> <br> <% end %>
<%= "#{a.company_name}" unless a.company_name.blank? %>
<% unless a.company_name.blank? %> <br> <% end %>
<%= "#{a.street_1}" unless a.street_1.blank? %>
<% unless a.street_1.blank? %> <br> <% end %>
<%= "#{a.street_2}" unless a.street_2.blank? %>
<% unless a.street_2.blank? %> <br> <% end %>
<%= "#{a.city}, #{a.state} #{a.zip}" %>
So, my gratuitous use of unless aside, how should I be putting in a conditional line break?
Update:
As discussed below, it is dangerous to use .html_safe on user input. If you do use a helper method as suggested below, you must also sanitize all user input on the way into the database. I've rewritten the code above as:
<% unless a.name.blank? %>
<%= a.name %>
<br>
<% end %>
<% unless a.company_name.blank? %>
<%= a.company_name %>
<br>
<% end %>
<% unless a.street_1.blank? %>
<%= a.street_1 %>
<br>
<% end %>
<% unless a.street_2.blank? %>
<%= a.street_2 %>
<br>
<% end %>
<%= "#{a.city}, #{a.state}" %> <%= a.zip %>
The redundant checking was just me overcomplicating things. I'd strongly recommend against using .html_safe in a situation like this, since you create new problems for yourself: sanitizing the input, and remembering which fields are safe. Better to not override the sensible protection Rails provides.
There are many, many ways to go about cleaning it up, but a helper would be appropriate here:
module ApplicationHelper
def format_address(a)
top = [a.name, a.company_name, a.street_1, a.street_2]
top.reject! {|s| s.blank?} # remove null and empty values
"#{top.join('<br/>')}#{a.city}, #{a.state} #{a.zip}".html_safe
end
end
Then in your view:
<%= format_address(a) %>

(not) displaying optional fields in rails

(Disclaimer: I'm a bloody rookie)
My model contains some optional values. It seems that they may not exist at all (==nil) or that they may exist but are empty. In both cases, I don't want to show anything in my view. Currently, I do it like this:
<% if #score.lyricist and not #score.lyricist.empty? %>
<p>
<strong>Lyricist:</strong>
<%= #score.lyricist %>
</p>
<% end %>
This seems awkward and repetitive. Is there a better way?
blank? method will check both nil and empty values
<% unless #score.lyricist.blank? %>
<p>
<strong>Lyricist:</strong>
<%= #score.lyricist %>
</p>
<% end %>
Documentation here
present? method is the opposite of blank? method.
<% if #score.lyricist.present? %>
<p>
<strong>Lyricist:</strong>
<%= #score.lyricist %>
</p>
<% end %>

Whats wrong with my simple If Else?

Im new to RoR/Ruby and i cant seem to get the simplest thing to work. (trust me, ive search google and reread docs, i dont know what wrong)
So in my main view, I added the following:
<%= if 1>2 %>
<%= print "helllloooo" %>
<%= else %>
<%= print "nada" %>
<%= end %>
And nothing is outputted..
**UPDATE**
Ok heres my new CORRECTED code and its STILL NOT WORKING
<th>
<% if 1 > 2 %>
<%= print "helllloooo" %>
<% else %>
<%= print "nada" %>
<% end %>
</th>
Your statements are not intended to be displayed so instead of
<%= if 1>2 %>
write
<% if 1 > 2 %>
Same thing for else and end
EDIT
<% if 1 > 2 %>
<%= "helllloooo" %> #option 1 to display dynamic data
<% else %>
nada #option 2 to display static data
<% end %>
You don't need to use print, or even ERB for the text. Also, your if, else, and end statements should be <%, not <%=:
<% if 1 > 2 %>
helllloooo
<% else %>
nada
<% end %>
<%= already means "print to the HTML response" in ERB (Ruby's own templating language).
So <%= print '...' means "print the return type of print '...'" which is nothing.
The right code would look like:
<% if 1>2 %>
<%= "helllloooo" %>
<% else %>
<%= "nada" %>
<% end %>
In fact you can even omit the <%= because you're just printing strings (not arbitrary objects):
<% if 1>2 %>
helllloooo
<% else %>
nada
<% end %>
The = is the problem. Use <% instead. <%= is for printing something, while <% is for instructions.
for dynamic content use: <%= %>

Resources