I have a action to download file csv, but when click error missing template
Code in my action
def export
respond_to do |format|
format.html
format.csv { send_data UserDetail.order('id desc').first(20).to_csv }
end
end
In my view:
= link_to "Export CSV", export_call_center_user_details_path(format: "csv"), :class => "btn btn-default"
error:
Processing by CallCenter::UserDetailsController#export as CSV
Completed 500 Internal Server Error in 5msA 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]}.
When i try remove line format.html
so error:
Completed 406 Not Acceptable in 4ms (ActiveRecord: 0.3ms)
How to fix error above?
update
error:
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)
SELECTadmins
.* FROMadmins
WHEREadmins
.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 [email protected] (1404ms) Date: Mon, 20 Jun 2016
16:10:33 +0700 From: xxx
To: [email protected] Message-ID:
<[email protected]> 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:
7bitA 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’
3
Answers
I guess that you miss
filename
andtype
:Have a look in your mine_type.rb file.
You may need to add the following.
Mime::Type.register "application/csv", :csv
The first thing we need to do is open up the config/application.rb file and add:
below the line that says:
Next, we need to add some code to the model that will export the data in the CSV format. Add the following code to the model you wish to export to CSV:
This code will export both the column headers as well as the data in the csv format and return the result.
Finally, we need to add a bit of extra code to our controller in order to return the CSV data to our user. Assuming your model and controller are named posts, add the following code to your posts controller:
Credits: Check