i am getting Missing template posts/download, application/download with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}. Searched in: * "/home/raj/Downloads/carrierwave/app/views" error while i am running my aplication.
This is my controller:
class PostsController < ApplicationController
require "tmpdir"
require 'zip'
TmpDir = "/path/to/tmp/dir"
before_action :set_post, only: [:show, :edit, :update, :destroy]
# action method, stream the zip
def download # silly name but you get the idea
generate_zip do |zipname, zip_path|
File.open(zip_path, 'rb') do |zf|
# you may need to set these to get the file to stream (if you care about that)
# self.last_modified
# self.etag
# self.response.headers['Content-Length']
self.response.headers['Content-Type'] = "application/zip"
self.response.headers['Content-Disposition'] = "attachment; filename=#{zipname}"
self.response.body = Enumerator.new do |out| # Enumerator is ruby 1.9
while !zf.eof? do
out << zf.read(4096)
end
end
end
end
end
# Zipfile generator
def generate_zip(&block)
invoice = Post.find(params[:post_id])
photos = invoice.post_attachments
# base temp File.dirname(__FILE__)
tmp_dir = Dir.mktmpdir
# path for zip we are about to create, I find that ruby zip needs to write to a real file
zip_path = File.join(tmp_dir , 'export.zip')
Zip::File::open(zip_path, true) do |zipfile|
photos.each do |photo|
zipfile.get_output_stream(photo.avatar.identifier) do |io|
io.write photo.avatar.file.read
end
end
end
# yield the zipfile to the action
block.call 'export.zip', zip_path
ensure
# clean up the tempdir now!
FileUtils.rm_rf tmp_dir if tmp_dir
end
in routes.rb:
get '/posts/download/:post_id' => 'posts#download', as: :download_post
and in my index file:
<% @posts.each do |post| %>
<%= link_to "Download", download_post_path(post.id) %>
<% end %>
I also checked in rake routes, i am getting:
download_post GET /posts/download/:post_id(.:format) posts#download
I don't know where it went wrong. Please help. And I don't want to use a template, it should take with only method.