How to keep style for HTML in Ruby/Rails? - html

I'm a beginner to Ruby/Rails, and just generated my first HTML programmatically - kind of exciting-- but when I viewed "page source" from the browser, my HTML had all these additional gaps and messed up the logical indentation:
This code in a View:
<% #states_array.each do |state| %>
<ul><%= state %></ul>
<% end %>
and this code in my application.html.erb layout:
Practice Header
<div class="text">
<%= yield %>
</div>
<div class="footer">
</div>
Produced this HTML when I viewed page source for that page:
<div class="header">
Practice Header
</div>
<div class="text">
<ul>California</ul>
<ul>Colorado</ul>
<ul>Florida</ul>
<ul>Georgia</ul>
<ul>New York</ul>
<ul>North Carolina</ul>
<ul>North Dakota</ul>
<ul>Oregon</ul>
</div>
<div class="footer">
</div>
only an extra space occurred after each row, and the logical indentation of where I put the <%= yield %> was lost. Thanks so much for your help in advance.

You can suppress the trailing newline by closing with a minus sign:
<% some.ruby.statement -%>
If the beauty of your markup really matters to you, look into Haml (http://haml-lang.com/).

<% #states_array.each do |state| %>
<ul><%= state %></ul>
<% end %>
Results in the string "\n<ul>state</ul>\n" for each state in the array. So the output is technically correct. You could use
<% #states_array.each do |state| %><ul><%= state %></ul>
<% end %>
But that's not as easy to read in your code. I've read there is a way to skip the trailing new lines but don't recall the exact method (Update: see #user156011's answer).
But the truth is - it doesn't really matter. HTML is for the browser - don't worry about how it looks. The only time you really need to pay attention is when two tags must exist one after the other without spacing to prevent browsers from injecting default whitespace - like in a series of tags sliced up from a larger image.

If you're going for markup readability - Haml has been nothing but a dream for me.
In development mode, it outputs gorgeous HTML that is properly indented.
It'll switch to "ugly mode" by default when your app is run in production mode, to save on server resources.
However, if you're new to Ruby/Rails, learning a new templating language may not be in your best interest. (Still, I'd argue that if you can learn ERb, you can easily pickup Haml in a day.)
If you're going to stick to ERb, you can use the <%- and -%> will respectively supress leading/trailing whitespace. Which may help in your quest for clean markup.
Best of luck :)
~Robbie

You should probably change it to something like:
<ul>
<% #states_array.each do |state| %>
<li><%= state %></li>
<% end %>
</ul>

Related

HTML vs text in rails app user_mailer

In my rails web app (based on the RoR tutorial by Michael Hartl) I have a user mailer which shoots out an activation email when someone signs up. There is an account_activation.*.erb for both HTML and text versions of the email. My account_activation.html.erb is
<h1>Web App</h1>
<p>Hi <%= #user.first_name %>,</p>
<p>Welcome to the Web App!</p>
<% if #user.student? %>
<p>
Once you have activated your account, you need to fill out the following information:
<ul>
...
</ul>
</p>
<% elsif #user.teacher? %>
<p>
You have been created as a teacher.
After activation, you will need to reset your password as it has been
randomly generated.
</p>
<% end %>
<p>Click on the link below to activate your account:</p>
<%= link_to "Activate", edit_account_activation_url(#user.activation_token,
email: #user.email) %>
What I'm wondering is: what the corresponding account_activation.text.erb would be since I have a conditional statement in the HTML?
You can still use interpolated Ruby in your .text.erb files - it's ERB that handles that, not HTML. Your template should look pretty much the same, just laid out for text without any HTML tags.
Since text file print all of your whitespace, you'll probably want to use <%- and -%> tags in some places, which respectively consume space before and after your interpolations. You'll also end up indenting less and not breaking things onto multiple lines, unless you use some tool to clean that up.
Web App
Hi <%= #user.first_name %>,
Welcome to the Web App!
<%- if #user.student? -%>
Once you have activated your account, you need to fill out the following information:
- One
- Two
<%- elsif #user.teacher? -%>
You have been created as a teacher. After activation, you will need to reset your password as it has been randomly generated.
<%- end -%>
Click on the link below to activate your account:
<%= link_to "Activate", edit_account_activation_url(#user.activation_token,
email: #user.email) %>

Is there a way to add HTML inside a Rails partial argument?

I have been trying to find something about embedding HTML code inside a partial argument for days but I have not found anything so I'm guessing it isn't possible. But it seems like it should be.
I have a static page in my Rails app which has a lot of sections and each section can have subsections. I could just make the entire page just plain HTML. But I didn't want to repeat the same formatting over and over in case I want to change classes or something else.
So I have the following _section.html.erb partial file:
<div class="row">
<h4><%= heading %></h4>
<% subsections.each do |section| %>
<% if section[:header] %>
<h5 class="primary-text"><%= section[:header] %></h5>
<% end %>
<p><%= section[:body] %>
<% end %>
</div>
That works fine. But what if I want to include a link to a page or an email inside one of the subsections? It doesn't work just by passing it in as part of the quotes text. It shows the actual HTML tags.
Is there a real way to do this or should I give up and just write plain HTML with repeated section formatting?
You mark your text as html_safe. For example:
<%= section[:header].html_safe %>
But I would suggest using sanitize method because of security resonons:
<%= sanitize section[:header] %>
Probably sometimes you will want to configure sanitize method. Here you can read how to do this:
http://api.rubyonrails.org/classes/ActionView/Helpers/SanitizeHelper.html
You can read more about security here:
http://guides.rubyonrails.org/security.html#cross-site-scripting-xss

how to either get rid of the commas added (by default) between tags in Refinery CMS?

My goal is to customize the tags for blog posts in the Refinery CMS. By default they are not styled. I added some styling just to test that I was making changes to the right areas (yes I know it is ugly).
But my goal intent is to get them in a format similar to what I have on my own blog.
Refinery is adding a comma after each tag. What should I modify in the html or css to not add the commas?
app/views/refinery/blog/shared/_tags.html.erb
<% if #tags.any? %>
<h2>Tag Cloud</h2>
<nav id='tags'>
<% tag_cloud(#tags, %w(tag1 tag2 tag3 tag4)) do |tag, css_class| %>
<%= link_to tag.name, refinery.blog_tagged_posts_path(tag.id, tag.name.parameterize), :class => css_class %>
<% end %>
</nav>
<% end %>
The tag_cloud method is a method found in the acts-as-taggable-on library that the refinerycms-blog extension uses. More information can be found out about it in the readme for the library.

How do I nest a div inside an anchor tag in rails

I'm a bit of a Ruby on Rails amateur, and I am trying to nest a div tag inside of an anchor tag in rails. I can make it work, but the resulting code I have written is terrible and is certainly NOT the rails way.
Here is an example of what I am trying to accomplish in HTML:
<a href="tell-a-friend">
<div id="tellafriend">
<strong>Strength in Numbers.</strong><br />
Suggest a friend or colleague to participate in this survey.
</div>
</a>
Here is what I came up with to do it in ERB:
<%= link_to content_tag(:div,
content_tag(:strong, 'Add your labor rates now.') +
content_tag(:br, '') + ' We are counting on you.', :id => 'participate'),
participate_path %>
Or here I mixed some HTML and ERB:
<%= link_to '<div id="results">
<strong>See the results.</strong><br />
Knowledge is power.
</div>'.html_safe, results_path %>
Both of my solutions seem very ugly... but moving it into a helper didn't seem like the right thing to do, given that the content of the DIV changes and that I am only displaying 3 of these on one page.
So if anyone is aware of a better way to do this, I am interested! Any combination of HTML, ERB and HAML is ok by me.
Links work as blocks:
<% link_to '', my_path do %>
<div></div>
<% end %>
FYI: An ANCHOR surrounding a DIV is not legal in HTML 4.01 Transitional (but it is in HTML5?) so make sure that you use the correct doc-type and test on the target browser(s)!
<head>
<title>a</title>
</head>
<body>
<div>
<a href="www.google.com">
<div>test</div>
</a>
</div>
</body>
</html>
You can run this at W3C Validator and change the DOCTYPE to see for which mode(s) this is valid. (Make sure to specify the encoding as well to get rid of the warning :-)
The worst is :
<%= link_to(url, html_options = {}) do %>
# name
<% end %>

what does dash mean in ruby on rails html scriptlet?

In the web I've seen examples both with
<% if #showIt -%>
some html content
<% end -%>
and without
<% if #showIt %>
some html content
<% end %>
dash. Both versions work very well. So, what difference does it make?
Thanks!
It's used to avoid inserting a newline after the code.
I found a very nice explanation here.
The above link seems broken, it is beautifully explained in this stack post