Reputation: 886
I'm trying to create a ROR application that allows me to upload excel files. I want to be able to list out the names of all excel files uploaded with a clickable link to each name that will bring me to the show.html.erb page which will show the data of the file.
I'm stuck with this "undefined method `find' for File:Class" error when i try to access the reports/index page, probably because there is not identifier associated with the file, but only with the row of the excel data.
Here is my schema:
ActiveRecord::Schema.define(version: 20170402043056) do
create_table "files", force: :cascade do |t|
t.string "Name"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "reports", force: :cascade do |t|
t.string "date"
t.string "order"
t.string "item"
t.string "product"
end
end
I have added this to routes.rb:
resources :reports do
collection { post :import }
end
Reports model:
`class Report< ActiveRecord::Base
require 'roo'
belongs_to :file
def self.import(file)
spreadsheet = open_spreadsheet(file)
header = spreadsheet.row(1)
(2..spreadsheet.last_row).each do |i|
row = Hash[[header, spreadsheet.row(i)].transpose]
report = find_by_id(row["id"]) || new
report.attributes = row.to_hash
report.save!
end
end
def self.open_spreadsheet(file)
case File.extname(file.original_filename)
when ".csv" then Roo::CSV.new(file.path, packed: false, file_warning: :ignore)
when ".xls" then Roo::Excel.new(file.path, packed: false, file_warning: :ignore)
when ".xlsx" then Roo::Excelx.new(file.path, packed: false, file_warning: :ignore)
else raise "Unknown file type: #{file.original_filename}"
end
end
end
reports_controller:
class ReportsController < ApplicationController
def index
@file = File.all
end
def show
@file = File.find(params[:id])
end
def import
@report = Report.import(params[:file])
@file = File.import(params[:id])
redirect_to reports_path, notice: "report imported."
end
end
new.html.erb (page to upload file):
<%= form_tag import_reports_path, multipart: true do %>
<%= file_field_tag :file %>
<%= submit_tag "Import" %>
<% end %>
index.html.erb (page to list out names of files uploaded):
<h1>List of reports uploaded</h1>
<table>
<thead>
<tr>
<th>Name</th>
<th>Created at</th>
<th>Updated at</th>
</tr>
</thead>
<tbody>
<% @report.each do |report| %>
<tr>
<td><%= report.original_filename %></td>
<td><%= report.created_at %></td>
<td><%= report.update_at %></td>
</tr>
<% end %>
</tbody>
</table>
show.html.erb (page showing the data):
<h1>Report data</h1>
<table>
<thead>
<tr>
<th>Date</th>
<th>Order</th>
<th>Item</th>
<th>Product</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<% @report.each do |report| %>
<tr>
<td><%= report.date %></td>
<td><%= report.order %></td>
<td><%= report.item %></td>
<td><%= report.product %></td>
</tr>
<% end %>
</tbody>
</table>
Upvotes: 0
Views: 1012
Reputation: 44370
File
is a bad name for your model. Do not give that name, it's built-in class and shouldn't be used.
Upvotes: 3
Reputation: 748
the reports/index page should be visited as /reports
, not /reports/index
.
The path /reports/index
, only match show
route reports/:id
, so it regards index
as the report id, so the exception happens.
Upvotes: 0