I am getting the following error, please any one view it and help me out of this..
Actually this is the error shown in my development.log file.
ActionView::Template::Error (undefined method `quantity' for #<LineItem:0xaa47b1c>):
4: <tr>
5: <% end %>
6: <tr>
7: <td><%= line_item.quantity %>×</td>
8: <td><%= line_item.product.title %></td>
9: <td class="item_price"><%= number_to_currency(line_item.total_price) %></td>
10: </tr>
app/views/line_items/_line_item.html.erb:7:in `_app_views_line_items__line_item_html_erb___826332857_89103730'
app/views/carts/_cart.html.erb:4:in `_app_views_carts__cart_html_erb___185026047_87767060'
app/views/layouts/application.html.erb:19:in `_app_views_layouts_application_html_erb___370633105_87583010'
app/helpers/application_helper.rb:6:in `hidden_div_if'
app/views/layouts/application.html.erb:18:in `_app_views_layouts_application_html_erb___370633105_87583010'
The error's quite clear: your LineItem class does not have a method (or database field) called quantity.
Related
I am using to_xls and spreadsheet gem to export data to excel sheet in rails4
However, I am not able to export the data to excel sheet even if the excel sheet fires open.
I am following this tutorial
http://code.dblock.org/2011/09/01/exporting-data-to-excel-in-ruby-on-rails-w-spreadsheet-and-toxls.html
orders_controller.rb
def index
#orders = Order.all.includes(:status,:document,:platform, payment:
[:status]).where(status_id: Orders::Status.where(name:
['INPROGRESS', 'COMPLETED']).pluck(:id)).paginate(:page =>
params[:page], :per_page => 30).order('id Desc')
#order_statuses = Orders::Status.all
#delivery_statuses = Orders::DeliveryStatus.all
respond_to do |format|
format.html
format.xls do
send_data(
Order.where(status_id:
Orders::Status.where(name:
['COMPLETED',
'INPROGRESS' ]).pluck(:id))
.includes(:status, :platform,
:payment).map{
|order|
{
id: order.id, txnid: order.txnid,
created_at: order.created_at,
product: order.product,
payment:
order.payment.status.name,
status: order.status.name,
total_amount: order.total_amount,
discount: order.discount,
platform: order.platform.name
}}.to_xls,
content_type: 'application/vnd.ms-excel',
filename: 'orders.xls' )
end
end
end
orders/index.html.erb
<table id='myOrdersTable'class="table table-bordered">
<thead>
<tr>
<th>Trans ID</th>
<th>Date(dd/mm/yy)</th>
<th>Product</th>
<th>Payment</th>
<th>Status</th>
<th>Total amount</th>
<th>Discount</th>
<th>Platform</th>
<th>Draft</th>
<th>Edit Draft</th>
<th>Upload Soft Copy</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #orders.each do |order| %>
<tr>
<td><%= order.txnid %></td>
<td><%= order.created_at.strftime('%d-%b-%Y') %></td>
<td><%= order.product %></td>
<td><%= order.payment.status.name if order.payment %></td>
<td><%= order.status.name %></td>
<td><%= order.total_amount %></td>
<td><%= order.discount %></td>
<td><%= order.platform.name %></td>
<td
:
:
:
<% end %>
</tbody>
</table>
<%= will_paginate #orders %>
:
:
<%#= link_to 'New Order', new_order_path %>
<%= link_to 'Exporting', "/admin/orders.xls", :class =>'btn btn-primary'
%>
Here in the link if call
<%= link_to 'Exporting', orders_path(request.parameters.merge({:format
=> :xls})), :class =>'btn btn-primary' %>
it gives a template missing error(Dont know why) .Hence I am not using the above link in the code to get the export button.
This helps me generate an excel sheet, but not able to export all the columns from the index.html.erb page and their values into the sheet. Just gives a blank spreadsheet. How can I export all these data in to sheet? Please help !I am a beginner so kindly elaborate and let me know if more data is needed.
My choice is to just manually generate CSV file like this :
#orders = Order.all
header = ["id", "status" // put your header field ]
csv = CSV.generate do |csv|
csv << header
#orders.each do |order|
row = [ordrer.id, order.total_amount //mention your fields here ]
end
csv << row
end
I am grouping mileage entries by month in my Rails 3.2 application:
mileages_controller.rb
def index
#mileages = Mileage.find_all_by_user_id(current_user.id)
#mileages_months = Mileage.find_all_by_user_id(current_user.id).group_by { |t| t.created_at.beginning_of_month }
respond_to do |format|
format.html # index.html.erb
format.json { render json: #mileages }
end
end
index.html.erb
<% #mileages_months.sort.each do |month, mileages| %>
<h4><%= month.strftime('%B %Y') %></h4>
<p>Total miles <strong><%= mileages.miles.sum %></strong>.</p>
<table>
<tr>
<th>Name</th>
<th>Miles</th>
<th>Start</th>
<th>Destination</th>
<th></th>
</tr>
<% for mileage in mileages %>
<tr>
<td><%= mileage.name %></td>
<td><%= mileage.miles %></td>
<td><%= mileage.start %></td>
<td><%= mileage.end %></td>
<td><%= link_to 'Show', mileage %></td>
</tr>
<% end %>
</table>
<% end %>
As you can see, I am trying to sum the total miles in each month using <%= mileages.miles.sum %>. This won't work - but <%= mileages.first.miles %> correctly shows the first entry's miles for each month.
What is the correct way of totalling up the miles column for each month?
Well you can create a helper method(to keep this logic seperate from the view) in the mileage_helper as :
def sum_mileges(mileages)
mileages.map(&:miles).sum
end
and then calling it in the view as
<p>Total miles <strong><%= sum_mileges(mileages) %></strong>.</p>
OR
you can do it directly as :
<p>Total miles <strong><%= mileages.map(&:miles).sum %></strong>.</p>
My app runs fine on my iMac in development.
But, I get the following error on Heroku (this is the Heroku log):
ActionView::Template::Error (undefined method `name' for nil:NilClass):
22: <% end %>
23: <td><%= workorder.description %></td>
24: <% if workorder.location_id != nil %>
25: <td><%= workorder.location.name %></td>
app/views/home/_myopenorders.html.erb:25:in `block in _app_views_home__myopenorders_html_erb__1134835514768073858_64792180'
app/views/home/index.html.erb:66:in `_app_views_home_index_html_erb___3327722386939287121_60092520'
26: <% else %>
27: <td></td>
app/views/home/_myopenorders.html.erb:15:in `_app_views_home__myopenorders_html_erb__1134835514768073858_64792180'
28: <% end %>
This is the code:
<% if workorder.location_id != nil %>
<td><%= workorder.location.name %></td>
<% else %>
<td></td>
<% end %>
Any ideas?
UPDATE:
Sometimes the log looks like Heroku is running the ruby code out of sequence. Look at this:
ActionView::Template::Error (undefined method `name' for nil:NilClass):
22: <% end %>
26: <% else %>
app/views/home/index.html.erb:66:in `_app_views_home_index_html_erb__2268065945584360823_49285320'
app/views/home/_myopenorders.html.erb:25:in `block in _app_views_home__myopenorders_html_erb___3485426105947531181_51160960'
27: <td></td>
app/views/home/_myopenorders.html.erb:15:in `_app_views_home__myopenorders_html_erb___3485426105947531181_51160960'
28: <% end %>
24: <% if workorder.location_id != nil %>
Sorry, my fault. A location got deleted and I didn't have the code to check whether the location was used by a workorder.
What is the tidiest DRY way in a Rails 3 application of highlighting a table row depending on its age?
<% #faults_open.each do |fault| %>
<tr>
<td><%= fault.name %></td>
<td><%= fault.faulttype %></td>
<td><%= fault.priority %></td>
<td><%= time_ago_in_words(fault.created_at) %> ago</td>
<td><%= link_to 'Show', fault %></td>
</tr>
<% end %>
I would like a class applied to the table row with an indication of its age which I can then style using CSS.
<% #faults_open.each do |fault| %>
<tr <% if fault.created_at > Date.1.week.ago %>class="weekold"<% end %>>
<td><%= fault.name %></td>
<td><%= fault.faulttype %></td>
<td><%= fault.priority %></td>
<td><%= time_ago_in_words(fault.created_at) %> ago</td>
<td><%= link_to 'Show', fault %></td>
</tr>
<% end %>
Something like the above but obviously adding them to the view would be messy. I plan on adding a couple of ages, such as:
weekold
twoweeksold
monthold
sixmonthold
yearold
ancient
I imagine a helper is the tidiest way, but I'm unsure on how the helper would work. Any pointers would be appreciated.
I recently did something similar, adding a class name to the row that corresponded to it's approximate age in weeks, then I used CSS to do the actual highlighting.
Why not use a helper.. (just an example)
def highlight_age_class (created_at)
##today ||= Date.today
num_weeks = ((##today - created_at.to_date) / 7).floor
class_name = "aged_#{num_weeks}_weeks"
num_weeks == 1 ? class_name.singularize : class_name
end
With your HTML..
<tr class="<%= highlight_age_class fault.created_at %>">
Combined with some CSS..
.aged_1_week
.aged_1_weeks
.aged_4_weeks
.aged_24_weeks
.aged_56_weeks
etc
You can then focus on extending and optimizing your helper method for age highlighting.
Basically I have a table in the index view where if a pictures name = army.unit_name, then it renders that picture. If not, it displays in text that the image is missing. I'm still new at Rails, but there must be a way to put this conditional check somewhere else other than the view. Could someone please enlighten me? Do I do it in the model?
<% #armies.each do |army| %>
<tr>
<% if File.exists?("/users/<name>/Ruby Projects/thrones/app/assets/images/#{army.unit_name}.png") %>
<td><span class="picture-hover"><%= image_tag("#{army.unit_name}.png" %></span></td>
<% else %>
<td>Unit Card art missing</td>
<% end %>
<td><%= link_to army.unit_name, army_path(army) %></td>
</tr>
<% end %>
You can try this
...
<% if Rails.application.assets.find_asset "#{army.unit_name}.png" %>
...