Convert from haml to erb - ruby-on-rails-3

I try to convert the code from haml to erb but I'm getting stuck and don't know why.
Here is the original code I want to convert: https://github.com/gmarik/simple-backend-example/blob/master/app/views/backend/resource/_index.html.haml
And here is what I have right now. Can someone take a look at it and give me some hints. Thanks.
I doubt this line the most:
%tr[resource]{odd_or_even}
I think that it might be like:
<tr> <% #resource{odd_or_even} %>
RubyMine gave me an error at this line:
<%= paginate collection %>
<% content_for(:header) do %>
<h1><%= resource_class.model_name.human(count: 2) %></h1>
<ul class="tabs">
<li class="active"><%= link_to "Index", "#" %></li>
<li><%= link_to "New", new_resource_path %> </li>
</ul>
<table class='zebra-striped'>
<thead>
<tr>
<% attributes.each do |attr| %>
<th> <%= resource_class.human_attribute_name(attr) %></th>
<th> </th>
</tr>
</thead>
<tbody>
<% collection.each do |resource| %>
<tr> <% #resource{odd_or_even} %>
<% attributes.each do |attr| %>
<td> <%= resource.public_send(attr).to_s.truncate(20) %> </td>
<td class='row-actions'>
<%= link_to 'show', resource_path(resource) %>
|
<%= link_to 'edit', edit_resource_path(resource) %>
|
<%= link_to 'destroy', resource_path(resource), method: :delete, confirm: "Are you sure?" %>
</td>
<% end %>
</tbody>
</table>
<%= paginate collection %>

Looking at the documentation here : http://haml.info/docs/yardoc/file.REFERENCE.html#object_reference_
I'd say it's :
<tr id="<%= "#{resource.class.name.underscore}_#{resource.to_key}" %>" class="<%= resource.class.name.underscore %>">
This is to translate %tr[resource].
Now, the {odd_or_even} will just convert the result hash of the helper odd_or_even to map them as attributes on the tr.
If we take a look at the definition of the method here : https://github.com/gmarik/simple-backend-example/blob/master/app/helpers/backend/application_helper.rb
We see it's just a call to cycle in order to set an extra class. Therefore we end up with:
<tr id="<%= "#{resource.class.name.underscore}_#{resource.to_key}" %>" class="<%= "#{resource.class.name.underscore} #{cycle("odd", "even", name: "rows")}" %>">
Now, all of this won't fix the problem of paginate. Add the error message if you're still stuck on this.

Related

Rails - how do I create a hyperlink to a page for a single record?

How do you make <%= post.name %> a link to its own page?
<% #posts.each do |post| %>
<tr>
<td><%= post.name %></td>
</tr>
<% end %>
similar what this code does, but without the string 'Show'
<td><%= link_to 'Show', post %></td>
what i always had in mind is like how php does it, you wrap it in
<a href="<?php phpcode() ?>">
how can i do similar result in ruby?
How about
<%= link_to post.name, post_path(post) %>
or even easier:
<%= link_to post.name, post %>

div_for within a table

I am trying to create a table that populates based on all of the records for a specific model.
Here is the index.html.erb file
<%= div_for(#departments, :class => "test") do |department| %>
<tr>
<td class="indexc1">
<%= department.name %>
</td>
<td class="indexc2">
<%= department.location %>
</td>
<td class="indexc3">
<%= department.date_completed %>
</td>
<td class="indexc4">
<ul class="action">
<li><%= link_to 'Edit Info', edit_department_path(department), :class=>"tlink3" %></li>
<li><%= link_to 'Edit Tasks', department_path(department), :class=>"tlink3" %></li>
<li><%= link_to 'Destroy', department, method: :delete, data: { confirm: 'Are you sure?' }, :class=>"tlink3" %></li>
</ul>
</td>
</tr>
<% end %>
This seems to properly create the table rows but does not put them into divs as expected.
I would like to be able to create one table row for each department and then be able to use AJAX to add or remove them.
If you want each row to be wrapped in a div, you'll need to add the div class and id to the tr tag, IE: (tr id="myid" class="class") rather than wrapping the div around the tr.
You can use the rails content_tag_for to generate this:
<% #departments.each do |department| %>
<%= content_tag_for(:tr, department, :class => "test") do %>
<td class="indexc1">
<%= department.name %>
</td>
<td class="indexc2">
<%= department.location %>
</td>
<td class="indexc3">
<%= department.date_completed %>
</td>
<td class="indexc4">
<ul class="action">
<li><%= link_to 'Edit Info', edit_department_path(department), :class=>"tlink3" %></li>
<li><%= link_to 'Edit Tasks', department_path(department), :class=>"tlink3" %></li>
<li><%= link_to 'Destroy', department, method: :delete, data: { confirm: 'Are you sure?' }, :class=>"tlink3" %></li>
</ul>
</td>
<% end %>
<% end %>

Rails For Loop View

I have the following code:
<tbody>
<%= Item.each do |item|=%>
<tr>
<th><%= item.rev =%></th> <=========
<th><%= item.name =%></th>
</tr>
<%= end =%>
</tbody>
However I am getting a syntax error on the inidcated line. There is data in the database(Test case). No idea what I am doing wrong.
The equals to signs you have are wrong. Try as below:
<tbody>
<% Item.each do |item|%>
<tr>
<th><%= item.rev %></th>
<th><%= item.name %></th>
</tr>
<% end %>
</tbody>
The <%= should only be used for expressions that need to be evaluated.
To help understand embedded ruby see this link
http://www.ruby-doc.org/docs/ProgrammingRuby/html/web.html
The expression for erb tags is <% #code %>
now if we want to print that tag too then we apply <%= #code %>
i.e. only one '=' sign is used and that too on left side.
Also in line each iterator there nothing can be printed, hence no '=' sign in
that line, similar is the case with tags containing 'end'.
Hence your code should look like
<tbody>
<% Item.each do |item| %>
<tr>
<th><%= item.rev %></th>
<th><%= item.name %></th>
</tr>
<% end %>
</tbody>

Rails 3 View - Rounding Up

I have a Rails view to display a grid by PERIOD and GENERAL LEDGER, and I want to display a cumulative total. In the case below, however, my #cumulative_total in the "TOTAL" column is rounding up to the nearest dollar, so no cents are shown, even though they are displayed correctly in the general_ledger columns. Any ideas what I'm doing wrong?
<% #cumulative_total = 0 %>
<div id="gl_crosstab">
<table>
<tr>
<th>Period</th>
<% #general_ledgers.each do |g| %>
<th><%= g.general_ledger_number %></th>
<% end %>
<th>Total</th>
<th>% Expended</th>
</tr>
<% #expected_billings.group_by(&:period_id).each do |eb| %>
<tr>
<td><%= eb[1].first.period.pe_number %></td>
<% eb[1].each do|p| %>
<td><%= number_to_currency(p.expected_amount) %></td>
<% end %>
<td>
<% #cumulative_total = #cumulative_total + eb[1].inject(0){|sum,billing| sum+billing.expected_amount.to_i} %>
<%= number_to_currency( #cumulative_total ) %> </td>
<td><%= number_to_currency((#cumulative_total/#sla.project_total)*100) %> % </td>
</tr>
<% end %>
<tr>
<td><b>Total Budget</td>
<% #total_expected_billings.each do |teb| %>
<td><b><%= number_to_currency(teb[1].inject(0){|sum,billing| sum+billing.expected_amount.to_i}) %></td>
<% end %>
<td><b><%= number_to_currency(#expected_billings.inject(0){|sum,billing| sum+billing.expected_amount.to_i}) %> </td>
<td><b><%= number_to_currency((#expected_billings.inject(0){|sum,billing| sum+billing.expected_amount.to_i}/#sla.project_total)*100) %> % </b></td>
</tr>
</table>
</div>
I think that is because you are converting the billing amount to 'integers' while displaying:
#expected_billings.inject(0){|sum,billing| sum+billing.expected_amount.to_i
If you change
billing.expected_amount.to_i
to
billing.expected_amount.to_f
it should work.

Rails submit button not working in Internet Explorer

Bizzarre issue I've been working that is beyond frustrating. The submit button on one of my edit views is not working in Internet Explorer. It is a pretty standard layout using input type = submit (not a customized button_to or something).
The button works fine in Safari, Chrome, and Firefox on both Mac and Windows.
Edit submit buttons work fine on other pages in IE
Using nested_form_for, with :html => {:multipart => true}
Submit button
<%= f.submit("update", :class=>"post_button")%>
Has anyone had issues with submit buttons not working in IE?
== update ==
Updated jQuery to make sure it wasn't a nested_form issue. I actually think the link may be not been working before I added the nested form. However, it has always been a multipart form.
View
<head>
<%= stylesheet_link_tag 'profile' %>
<%= javascript_include_tag "jquery.min", "nested_form" %>
</head>
<%= nested_form_for(#profile, :html => {:multipart => true}) do |f| %>
<div class="content">
<div class="editprofilesub">
<div class="profile_title"> basic </div>
<table>
<tr>
<td class="profileformright">
<%= f.label :firstname, "First Name" %>
</td>
<td class="profileformleft">
<%= f.text_field :firstname, :class => "profilefield", :class=>"profilefield" %>
</td>
</tr>
<tr>
<td class="profileformright">
<%= f.label :lastname, "Last Name" %>
</td>
<td class="profileformleft">
<%= f.text_field :lastname, :class => "profilefield" %>
</td>
</tr>
<td class="profileformright">
<%= f.label :avatar, "User Image" %><br>
</td>
<td class="profileformleft">
<i>Please rotate images prior to uploading.</i> <br>
<%= f.file_field :avatar%>
</td>
</table>
<div class="profile_title"> location </div>
<table>
<tr>
<td class="profileformright">
<%= f.label :city %>
</td>
<td class="profileformleft">
<%= f.text_field :city, :class => "profilefield" %>
</td>
</tr>
</table>
<br>
<table>
<tr>
<td class="profileformright">
<%= f.label :state, "State" %>
</td>
<td class="profileformleft">
<%= f.select :state, Carmen.state_names(),{}, :class=> "state" %>
</tr>
</table>
<div class="profile_title"> about </div>
<table>
<tr>
<td class="profileformright">
<%= f.label :bio, "about" %>
</td>
<td class="profileformleft">
<%= f.text_area :bio, :rows => '5', :cols => '60', :class => "profilefield" %><br>
</td>
</tr>
<tr>
<td class="profileformright">
<%= f.label :dob, "Date of Birth" %>
</td>
<td class="profileformleft">
<%= f.date_select :dob,
{:start_year => Time.now.year,
:end_year => 1900,
:order => [:month, :day, :year]}%>
</td>
</tr>
</table>
</div>
<div class="right">
<%= f.submit("update", :class=>"post_button")%>
</div>
</div>
<%end%>
I just found a similar issue in my non-MVC application, in IE8 and below. When enter is used, the server-side click handler for the submit button wasn't being executed. The problem was that IE doesn't POST the submit button when enter is used (but does when it is clicked). So, .NET knows it's a post back, but not what control caused it.