简体   繁体   English

无法通过Rail应用程序导出csv文件

[英]can not export a csv file through a rail application

I am trying to export a csv file through a rail application. 我正在尝试通过铁路应用程序导出一个csv文件。 I have a previous application which imports the data from a csv file and shows it in the webpage. 我有一个以前的应用程序,它从csv文件导入数据并将其显示在网页中。 That application was fired from the url http://localhost:3000/ and it was working fine. 该应用程序是从URL http:// localhost:3000 /触发的,并且运行正常。

But now I am trying to get back the data shown in the webpage back to a csv file which I can download from the page. 但是现在,我试图将网页中显示的数据恢复为一个csv文件,该文件可以从该页面下载。

Now I am not sure why I am facing the error. 现在,我不确定为什么会遇到该错误。 The localhost page is not working and server is running in a loop. 本地主机页面不起作用,并且服务器正在循环运行。

I think I have some problems with in the user.rb file in all.each do section. 我认为在all.each do部分的user.rb文件中存在一些问题。

Actually I want to add a csv file download link in the same page. 实际上,我想在同一页面中添加一个csv文件下载链接。

My code is as follows:- 我的代码如下:

I created my app name is names My app\\controllers\\users_controller.rb file is: 我创建的应用程序名称为名称,我的app \\ controllers \\ users_controller.rb文件为:

class UsersController < ApplicationController
def index
 @users=User.all

 @users= User.order(:name)
 respond_to do |format|
  format.html { redirect_to root_url }
  format.csv {send_data @users.to_csv}

 end

end def import User.import(params[:file]) redirect_to root_url, notice: "Activity data imported!" end def import User.import(params [:file])redirect_to root_url,注意:“活动数据已导入!” end end 结束

My app\\models\\user.rb file is: 我的app \\ models \\ user.rb文件是:

class User < ActiveRecord::Base
require 'csv'

def self.to_csv(options = {}) 
CSV.generate(headers: true) do |csv|
    csv << column_names
      all.each do |name|
        csv << name.attributes.values_at(*column_names)
      end
end
end




def self.import(file)
quote_chars = %w(" | ~ ^ & *)
begin
    CSV.foreach(file.path, headers: :first_row, quote_char:     quote_chars.shift) do |row|
    User.create! row.to_hash
end

rescue CSV::MalformedCSVError
    quote_chars.empty? ? raise : retry 
 end
end
end

My app\\views\\users\\index.html.erb file is: 我的app \\ views \\ users \\ index.html.erb文件是:

<%= flash[:notice] %>

<p>
Download:
<%= link_to "CSV", names_path(format: "csv") %> |

</p>

<table>
<thead>
    <tr>
        <th>Age</th>
        <th>Name</th>
    </tr>
</thead>

<tbody>
    <% @users.each do |user| %>
    <tr>
        <td><%= user.age %></td>
        <td><%= user.name %></td>
    </tr>
    <% end %>
</tbody>
</table>

<div>
<h4>Import the data!</h4>
<%= form_tag import_users_path, multipart: true do %>
<%= file_field_tag :file %>
<%= submit_tag "Import CSV" %>
<% end %>
</div>

My previous application database screenshot is:- 我以前的应用程序数据库屏幕截图是: 列名称分别为Age和Name

You are not following Rails conventions. 您没有遵循Rails约定。 You defined name controller, model and view but you didn't define routes for them. 您定义了名称控制器,模型和视图,但未定义它们的路由。 Something like resources :names or just for index action get '/names' => 'names#index' On the other hand you have routes defined for user, with resources :users, so you need to have UsersController(if you have defined users_controller check file name for it, this error may happen if you mistyped name for it). resources :names或仅用于索引操作的东西get '/names' => 'names#index'另一方面,您为用户定义了带有资源:users的路由,因此您需要有UsersController(如果已定义users_controller检查文件名,如果您输入了错误的名称,则可能会发生此错误)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM