Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 未定义的方法'find';文件:类_Ruby On Rails - Fatal编程技术网

Ruby on rails 未定义的方法'find';文件:类

Ruby on rails 未定义的方法'find';文件:类,ruby-on-rails,Ruby On Rails,我正在尝试创建一个ROR应用程序,允许我上传excel文件。我希望能够列出所有上传的excel文件的名称,并通过一个指向每个名称的可单击链接将我带到show.html.erb页面,该页面将显示文件的数据 当我试图访问报告/索引页面时,我遇到了“未定义的方法‘find’for File:Class”错误,这可能是因为没有与文件关联的标识符,而仅与excel数据行关联 这是我的模式: ActiveRecord::Schema.define(version: 20170402043056) do

我正在尝试创建一个ROR应用程序,允许我上传excel文件。我希望能够列出所有上传的excel文件的名称,并通过一个指向每个名称的可单击链接将我带到show.html.erb页面,该页面将显示文件的数据

当我试图访问报告/索引页面时,我遇到了“未定义的方法‘find’for File:Class”错误,这可能是因为没有与文件关联的标识符,而仅与excel数据行关联

这是我的模式:

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
我已将此添加到routes.rb:

resources :reports do
  collection { post :import }
end
报告模型:

`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
`class Report
报告和控制器:

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(要上载文件的页面):


index.html.erb(列出上载文件名称的页面):

已上载的报告列表
名称
创建于
更新于
show.html.erb(显示数据的页面):

报告数据
日期
命令
项目
产品

报告/索引页面应作为
/reports
访问,而不是
/reports/index


路径
/reports/index
,只匹配
show
route
reports/:id
,因此它将
index
视为报告id,因此发生异常。

报告/索引页面应作为
/reports
访问,而不是
/reports/index


路径
/reports/index
,只匹配
show
route
reports/:id
,因此它将
index
视为报告id,因此会发生异常。

对您的模型来说是个坏名字。不要给这个名字,它是内置类,不应该使用。

对您的模型来说是个坏名字。不要使用该名称,它是内置类,不应使用。

好的,谢谢。我已经编辑了我的代码。我正在尝试捕获已上载文件的名称,并将其显示在index.html.erb中。我使用了一个has_one关联。上载成功,但未显示任何文件名。。。正如我所期望的,因为在上传过程中没有任何东西存储在title_name中。这是另一个问题,也是另一个问题。问题1问题1答案,这就是Stackoverflow的工作原理。好的,很抱歉。我在这里提出了另一个问题:好的,谢谢。我已经编辑了我的代码。我正在尝试捕获已上载文件的名称,并将其显示在index.html.erb中。我使用了一个has_one关联。上载成功,但未显示任何文件名。。。正如我所期望的,因为在上传过程中没有任何东西存储在title_name中。这是另一个问题,也是另一个问题。问题1问题1答案,这就是Stackoverflow的工作原理。好的,很抱歉。我在这里提出了另一个问题:
<%= form_tag import_reports_path, multipart: true do %>
  <%= file_field_tag :file %>
  <%= submit_tag "Import" %>
<% end %>
<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>
<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>