現在、carrier_waveを使用してサイトのダウンロードと表示をしたく考えています。
アップロードは問題なくプログラムできたのですが、ダウンロードが上手く行かずに困っています。
以下、プログラムとなります。
修正の指摘をお願いできないでしょうか?
routes
1 resources :companies do 2 resources :comments 3 resources :attachments 4 member do 5 get 'attachments/view' 6 get 'attachments/download/:id' => 'attachments#download', as: :attachments_pdf 7 end 8 end
model
1class Attachment < ApplicationRecord 2 belongs_to :company 3 mount_uploader :file, UploadFileUploader 4end
controller
1 def download 2 @attachment = Attachment.find(params[:id].to_i) 3 filepath = @attachment.file.current_path 4 stat = File::stat(filepath) 5 send_file(filepath, :filename => @upload_file.file.url.gsub(/.*//,''), :length => stat.size) 6 end 7 8 def view 9 @attachment = Attachment.find(params[:id].to_i) 10 filepath = @attachment.file.current_path 11 stat = File::stat(filepath) 12 send_file(filepath, :filename => @upload_file.file.url.gsub(/.*//,''), :length => stat.size, :disposition => 'inline') 13 end
show
1<h2 class="headline">添付ファイル</h2> 2<table class="company"> 3 <tbody> 4 <% if @company.attachments.any? %> 5 <% @company.attachments.each do |attachment| %> 6 <tr> 7 <th width="80%"><%= attachment.name %></th> 8 <td width="20%"><%= attachment.created_at %></td> 9 <td width="10%"><%= link_to 'view', attachments_view_mypage_company_path(attachment), {action: "view", id: attachment.id} %></td> 10 <td width="10%"><%= link_to 'download',attachments_pdf_mypage_company_path(attachment), :class => 'btn btn-default btn-xs' %></td> 11 <td><td width="10%"><%= link_to "削除", 12 mypage_company_attachment_path(company_id: @company.id, id: attachment.id), 13 method: :delete, 14 class: :"btn btn-primary", 15 data: { confirm: '本当に削除しますか?'} %></td> 16 </tr> 17 <% end %> 18 <% end %> 19 <%= form_for ([:mypage, @company, @company.attachments.build]) do |f| %> 20 <tr> 21 <td width="45%" colspan="2"><%= f.text_field :name %></td> 22 <td width="35%" colspan="2"><%= f.file_field :file %></td> 23 <td width="20%"><%= f.submit '登録' %></td> 24 </tr> 25 </tbody> 26</table> 27<% end %>
あなたの回答
tips
プレビュー