Rails HAML how to do a simple loop? [closed] - ruby

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
How to do a simple HAML loop. That would be like this in ERB view:
<table>
<tr>
<th></th>
<th></th>
</tr>
<%= #price. each do |row| %>
<tr>
<td><%= row.year %></td>
</tr>
<% end %>
</table>
Example just tried this:
%table
%tbody
%tr
%th year
%th price
Error:
Inconsistent indentation: 3 spaces were used for indentation

In HAML the indentation matters, and it means the code nesting, which allows you not to use closing tags and end statements in Ruby code. To make HAML understand your nesting properly, all the indents need to be the same size, 2 spaces for example.
So, your ERB code will look the following:
%table
%tr
%th
%th
- #price. each do |row|
%tr
%td= row.year

Related

From UTC time to a readable format usin haml

Im currently working with the mirror quick start demo from Google Glass, which is built in Ruby using Sinatra to make the web app. Also it uses haml to render the website. My question is:
I can get the date from the timeline card using
Rack::Utils.escape_html(timeline_item.created)
But it give me back the UTC time. I already tried using strftime but its not working (it says it's a String).
Yeah I'm new on this, Any help? please??
heres the code of the layout file on haml
.span4
%table.table.table-bordered
%tbody
%tr
%th Attachments
%td
- if timeline_item.attachments.length > 0
- timeline_item.attachments.each do |attachment|
%img{src: "/attachment-proxy?timeline_item_id=#{timeline_item.id}&attachment_id=#{attachment.id}"}
- else
.muted None
%tr
%th Text
%td= Rack::Utils.escape_html(timeline_item.text)
%tr
%th HTML
%td= Rack::Utils.escape_html(timeline_item.created)
%tr
%td(colspan="2")
%form.form-inline(method="post" action="/delete-item")
%input(type="hidden" name="id" value="#{timeline_item.id}")
%button.btn.btn-block.btn-danger(type="submit") Delete
i suspect this might help
%td= timeline_item.created.strftime("%B %d, %Y")
meaning, don't use the escape_html thing on your time objects, that will convert it into a String and you'll get the crash you had

What does <%anything%> do?

In Rails tutorials and vids, it seems totally different from regular ruby. For example, I see <%render%> or <%end%>. When am I supposed to use <%%> and what is it for?
<% code %> is used in erb, which stands for "Embedded Ruby". It's typically used in HTML generating templates. It's brother is <%= code %> which outputs the return value of the expression inside it.
<h1>Hello, <%= #user.name %>!</h1>
# potentially renders: <h1>Hello, Bob</h1>
# potentially renders: <h1>Hello, Sue</h1>
The non-outputting <% code %> version of this tag is useful for executing code, but not writing anything to the template. This is useful for conditionals (as well as other things).
<h1>
Hello
<% if #user.sex == 'male' %>
Mister
<% else %>
Miss
<% end %>
<%= #user.name %>!
</h1>
# potentially renders: <h1>Hello Mister Bob!</h1>
# potentially renders: <h1>Hello Miss Sue!</h1>
In pure ruby, this would be a syntax error. But within an erb template, these tags allow you to control how the template renders by executing ruby to control the template flow, and by writing out the result of ruby expressions.
Rails uses erb by default for it's views, which are mostly html generating templates. So you see this a lot in Rails examples. Just keep in mind that erb is just one option for your templates in Rails. There is a great many options, which may use different syntax entirely.
That is used in HTML code to display ruby code inside the tags.
<%= render 'folder/partial_form' %>
That will render a form partial.
I suggest you have a nice long read through the links below:
http://www.guides.rubyonrails.org
http://guides.rubyonrails.org/layouts_and_rendering.html#using-partials
The <%= %> and <% %> syntax is used to write ruby in a .erb file. Think of ERB as an html file that always allows ruby code to be run. The <% %> syntax runs the ruby but does nothing by default while the <%= %> syntax(notice the =) outputs the result of the ruby code to the screen.

rendering Ruby statements inside of rails views?

So in my DB I want stored a ruby/HTML statement in the Database Table such as -
p This site is owned from 2000 - #{Time.now.year} by Acme Widget Co. /p
Or let's say that I want people to be able to include in other code snippits - e.g.
A client wants to run in a show view in the middle of a paragraph some magical partial such as #{render 'my_magical_code'}
On the view I have this being rendered as <%= raw(#page.content) %>
But its like a double rendering and rails will just put on the page #{render 'my_magical_code'} or #{Time.now.year}
So can / how do I solve this?
Thanks!
There are work arounds but I think the most proper way to solve your problem is to store your information in a better way. Add a migration to your model with a year_founded and company rows. You can then call these in your views rather than storing the HTML. Eg. This site is owned from <%= #object.year_founded %> to <%= Time.now.year %> by <%= #object.company %>

MissingTemplate : Render partial in Rails 3.2.3 [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
Hi I am new to Ruby on Rails and I am following Michael Hartl's book online.
In Partials section of his book. The code he used to render partial was <%= render 'layouts/stylesheets' %>
but I get this error.
I read the API and tried this <%=render :partial => "/layouts/stylesheets" %> but still can't figure this one out.
Thanks for all the help!
In your view, make sure you have the following structure:
- views
- layouts
- application.html.erb
- _stylesheets.html.erb
Your code should be:
<%= render 'layouts/stylesheets' %>
If your main template is inside the layouts folder:
<%= render 'stylesheets' %>
I overlooked the filename. When I initially created the file, after the .erb extension, I accidentally put a space after.

What's the best practice when indenting ERB control structures in HTML markup?

WHen you are writing ERB control structures in HTML markup, is it better to indent things to make the ERB logic clearer, or to make the resulting HTML indentation correct?
E.g.
<ul>
<li>Entries</li>
<% entries.each do |entry| %>
<li><%= entry.title %></li>
<% end %>
</ul>
vs
<ul>
<li>Entries</li>
<% entries.each do |entry| %>
<li><%= entry.title %></li>
<% end %>
</ul>
It's a taste-based preference, but I would go with indentation that makes the code easiest to read, since that's where more of your brain cells are expended. For me, that's the second option, since it is more obvious to me that there is a loop there, and that the loop is inside the list, not just some plain old unlooped <li> tags.
This is quadrupally (can one say that?) true when you think about all the stuff that can happen to the markup between your code and the response: have you ever tried to correctly guess the indentation of a rendered partial? It's pretty much by definition impossible. What about if your markup is minified?
HTML indents only really matter if you spend a lot of time reading your HTML page, but in practice, I only look at the HTML of my pages in my browser's developer tools, which automatically does some cleanup of the markup anyway.

Resources