gpt4 book ai didi

ruby-on-rails - 响应 CSV 时错过模板

转载 作者:数据小太阳 更新时间:2023-10-29 08:30:40 25 4
gpt4 key购买 nike

我有一个下载文件 csv 的操作,但是当点击错误时 missing template

我的操作中的代码

def export 
respond_to do |format|
format.html
format.csv { send_data UserDetail.order('id desc').first(20).to_csv }
end
end

在我看来:

= link_to "Export CSV", export_call_center_user_details_path(format: "csv"), :class => "btn btn-default"

错误:

Processing by CallCenter::UserDetailsController#export as CSV Completed 500 Internal Server Error in 5ms

A ActionView::MissingTemplate occurred in user_details#export:

Missing template call_center/user_details/export, application/export with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee, :haml]}.

当我尝试删除行 format.html所以错误:

Completed 406 Not Acceptable in 4ms (ActiveRecord: 0.3ms)

如何解决上面的错误?

更新

错误:

Started GET "/call_center/user_details/export.csv" for 127.0.0.1 at 2016-06-20 16:10:32 +0700 Processing by CallCenter::UserDetailsController#export as CSV Admin Load (0.3ms) SELECT admins.* FROM admins WHERE admins.id = 3 LIMIT 1 Completed 500 Internal Server Error in 53ms
Rendered /home/thekop/.rvm/gems/ruby-2.1.4/gems/exception_notification-2.6.1/lib/exception_notifier/views/exception_notifier/_request.text.erb (0.8ms) Rendered /home/thekop/.rvm/gems/ruby-2.1.4/gems/exception_notification-2.6.1/lib/exception_notifier/views/exception_notifier/_title.text.erb (0.3ms)
Rendered /home/thekop/.rvm/gems/ruby-2.1.4/gems/exception_notification-2.6.1/lib/exception_notifier/views/exception_notifier/_session.text.erb (1.0ms)
Rendered /home/thekop/.rvm/gems/ruby-2.1.4/gems/exception_notification-2.6.1/lib/exception_notifier/views/exception_notifier/_title.text.erb (0.1ms) Rendered /home/thekop/.rvm/gems/ruby-2.1.4/gems/exception_notification-2.6.1/lib/exception_notifier/views/exception_notifier/_environment.text.erb (9.5ms)
Rendered /home/thekop/.rvm/gems/ruby-2.1.4/gems/exception_notification-2.6.1/lib/exception_notifier/views/exception_notifier/_title.text.erb (0.1ms)
Rendered /home/thekop/.rvm/gems/ruby-2.1.4/gems/exception_notification-2.6.1/lib/exception_notifier/views/exception_notifier/_backtrace.text.erb (0.5ms)
Rendered /home/thekop/.rvm/gems/ruby-2.1.4/gems/exception_notification-2.6.1/lib/exception_notifier/views/exception_notifier/_title.text.erb (0.1ms)
Rendered /home/thekop/.rvm/gems/ruby-2.1.4/gems/exception_notification-2.6.1/lib/exception_notifier/views/exception_notifier/exception_notification.text.erb (21.4ms)

Sent mail to deekautest@gmail.com (1404ms) Date: Mon, 20 Jun 2016 16:10:33 +0700 From: xxx To: deekautest@gmail.com Message-ID: <5767b30931faa_ca17df30c996ab@thekop.mail> Subject: [xxx xxx] user_details#export (ActionView::MissingTemplate) "Missing template call_center/user_details/e... Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit

A ActionView::MissingTemplate occurred in user_details#export:

Missing template call_center/user_details/export, application/export with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee, :haml]}. Searched in: * "/home/thekop/rails/xxx/app/views/templates/xxx" * "/home/thekop/rails/xxx/app/views" * "/home/thekop/.rvm/gems/ruby-2.1.4/gems/devise-3.5.2/app/views" * "/home/thekop/.rvm/gems/ruby-2.1.4/gems/twitter-bootstrap-rails-2.2.4/app/views"

actionpack (3.2.13) lib/action_view/path_set.rb:58:in `find'

最佳答案

我们需要做的第一件事是打开 config/application.rb 文件并添加:

require 'csv'

在下面写着:

require 'rails/all'

接下来,我们需要向模型中添加一些代码,以将数据导出为 CSV 格式。将以下代码添加到您希望导出为 CSV 的模型中:

def self.as_csv
CSV.generate do |csv|
csv << column_names
all.each do |item|
csv << item.attributes.values_at(*column_names)
end
end
end

此代码将导出列标题以及 csv 格式的数据并返回结果。

最后,我们需要向我们的 Controller 添加一些额外的代码,以便将 CSV 数据返回给我们的用户。假设您的模型和 Controller 名为 posts,将以下代码添加到您的 posts Controller :

def index
@details = UserDetail.order('id desc').first(20)

respond_to do |format|
format.html
format.csv { send_data @details.as_csv }
end
end

致谢:Check

关于ruby-on-rails - 响应 CSV 时错过模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37915406/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com