我正在使用rails4
与paperclip
和rails_admin
宝石,我正在尝试建立一个链接,点击后会从提交表中下载所有附件。
这是我的代码
rails_admin/main/index.html.haml
- if @abstract_model.table_name == "submissions"
= link_to "Download all", '/downloadAttachments'
submission_controller.rb
# Download zip file of all submission
def download
@submissions = Submission.all
file = @submissions[0].file.url(:original, false) #folder to save the archive to
Zip::ZipFile.open(file, create=nil) do |zipfile|
@submissions.each do |filename|
zipfile.add(filename.file_file_name, filename.file.url(:original, false))
end
end
end
routes.rb
get '/downloadAttachments' => 'submissions#download'
当我点击链接时,我收到一条消息Cannot find submission with id downloadAll
。根据我对rails的经验,应该调用该方法并为我创建可下载的zip。问题:这个def有什么问题,如何下载该zip文件?
[编辑]
此代码似乎有效,但它获得了:
File /files/submissions/files/45/original/file.pdf not found
但我可以确认该文件确实存在于我的公共文件夹中
答案 0 :(得分:0)
这是解决方案:
# Download zip file of all submission
def download
@submissions = Submission.all
archiveFolder = Rails.root.join('tmp/archive.zip') #Location to save the zip
# Delte .zip folder if it's already there
FileUtils.rm_rf(archiveFolder)
# Open the zipfile
Zip::ZipFile.open(archiveFolder, Zip::ZipFile::CREATE) do |zipfile|
@submissions.each do |filename|
zipfile.add(filename.file_file_name, 'public/files/submissions/files/' + filename.id.to_s + '/original/' + filename.file_file_name)
end
end
# Send the archive as an attachment
send_file(archiveFolder, :type => 'application/zip', :filename => '2016 Submissions.zip', :disposition => 'attachment')
end
routes.rb
get '/downloadAttachments' => 'submissions#download'
= link_to "Download All", '/downloadAttachments'
在视图中:
ERROR! unexpected parameter type in action: <class 'ansible.parsing.yaml.objects.AnsibleSequence'>
The error appears to have been in '/root/myplaybook.yml': line 17, column 7, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
# configure rsyslog
- name: configure rsyslog to expose events on port 42000
^ here
答案 1 :(得分:-2)
我认为你的link_to代码格式错误。第二个参数应该是路径/路径:
= link_to "Download all", {:controller => "/submissions", :action => "download"}
或
= link_to "Download all", download_submissions_path