Rails For Loop View - ruby-on-rails-3

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>

Related

Logged out users are seeing error message "undefined method `leads' for nil:NilClass"

I made a simple app that lets users track their sales leads. Everything works except for the home page for logged out users, who get the error message - undefined method `leads' for nil:NilClass.
The index in the leads controller:
def index
#leads = current_user.leads.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #leads }
end
end
The homepage:
<% if user_signed_in? %>
<div class="row">
<div class="span3">
<h2>Leads</h2>
</div>
<div class="pull-right">
<%= link_to 'Add New Lead', new_lead_path, class: 'btn btn-primary' %>
</div>
</div>
<div>
<table id="leads" class="display">
<thead>
<tr>
<th>Company</th>
<th>Contact</th>
<th>Phone</th>
<th class="hidden-tablet hidden-phone hidden-desktop-small">Website</th>
<th class="hidden-tablet hidden-phone hidden-desktop-small">Email</th>
<th class="hidden-tablet hidden-phone hidden-desktop-small">Notes</th>
<th class="hidden-tablet hidden-phone hidden-desktop-small">Last Updated</th>
</tr>
</thead>
<tbody>
<% #leads.each do |lead| %>
<tr>
<td><%= link_to lead.company_name, lead %></td>
<td><%= lead.contact %></td>
<td><%= lead.phone %></td>
<td class="hidden-tablet hidden-phone hidden-desktop-small"><%= lead.website %></td>
<td class="hidden-tablet hidden-phone hidden-desktop-small"><%= lead.email %></td>
<td class="hidden-tablet hidden-phone hidden-desktop-small"><%= lead.notes %></td>
<td class="hidden-tablet hidden-phone hidden-desktop-small"><%= lead.updated_at.strftime("%d %b %y") %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
<% else %>
<div class="hero-unit">
<h1>Welcome to SnapLeads</h1>
<p>
The simplest CRM ever.
</p>
<p>
<%= link_to "Sign up now!", new_user_registration_path, class: "btn btn-primary btn-large" %>
</p>
</div>
<% end %>
Why would the page be trying to access the leads method if the user is logged out?
Because the code in your index action is always executed. You could use something like the following in your controller:
def index
#leads = current_user ? current_user.leads.all : nil
respond_to do |format|
format.html # index.html.erb
format.json { render json: #leads }
end
end

Convert from haml to erb

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.

Loop and generate table cells

I have a table that looks like the following:
<table class = "rota">
<thead>
<th>Date</th>
<% #hospitals.each do |hosp| %>
<th><%= hosp.name%></th>
<% end %>
</thead>
<tbody>
<%- if #rota_days.blank? %>
<tr>
<td colspan="<%= #hospitals.count %>">No rota day</td>
</tr>
<% end -%>
<% #dates.each do |date| %>
<tr>
<td><%= date.inspect %></td>
<% end %>
</tr>
</tbody>
</table>
Which outputs the following:
I am trying to generate blank rows that contain empty cells. I cannot seem to identify where I am going wrong. What is the best possible solution
Updated version
<table class = "rota">
<thead>
<th>Date</th>
<% #hospitals.each do |hosp| %>
<th><%= hosp.name%></th>
<% end %>
</thead>
<tbody>
<%- if #rota_days.blank? %>
<tr>
<td colspan="<%= #hospitals.count %>">No rota day</td>
</tr>
<% end -%>
<% #dates.each do |date| %>
<tr>
<td><%= date.inspect %></td>
<% (1..#hospitals.count).each do %>
<td></td>
<% end %>
</tr>
<% end %>
</tbody>
</table>
You're ending your loop too soon and you'll need a td for each th so change:
<% #dates.each do |date| %>
<tr>
<td><%= date.inspect %></td>
<% end %>
</tr>
To:
<% #dates.each do |date| %>
<tr>
<td><%= date.inspect %></td>
<% (1..#hospitals.count).each do %>
<td></td>
<% end %>
</tr>
<% end %>

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.