現在ファイルをデーターベース及び、S3に保存することはできているのですが、(
S3で再生も確認済み)音声ファイル及び、テキストファイルなどのファイルをviewに出力させることがうまくいっておりません。
ファイルの種類は
ruby
1 validates_attachment :file, content_type: { content_type: ['audio/mpeg', 'audio/x-mpeg', 'audio/mp3', 'audio/x-mp3', 'audio/mpeg3', 'audio/x-mpeg3', 'audio/mpg', 'audio/x-mpg', 'audio/x-mpegaudio', 'video/mp4', 'text/plain', 'application/msword', 'application/vnd.ms-powerpoint', 'application/pdf', "image/jpg", "image/jpeg", "image/png", "image/gif"] }
以上のファイルです。
これらを、出力させることがうまくいかず、今はまっているところです。
出力できないファイルは、音声ファイルとテキストファイルです。ビデオ及び画像の出力はうまくいっております。
保存方法は、
ruby
1<%= form_for @listing do |f| %> 2 <div class="col-md-5"> 3 4 <div class="row"> 5 <div class="col-md-12 select"> 6 <div class="form-group"> 7 <%= f.file_field :file, class: 'form-control' %> 8 </div> 9 </div> 10 </div> 11 12 13 <div class="save" style="float:right;"> 14 <%= f.submit "Save", class: "btn btn-danger" %> 15 </div> 16 </div> 17 <% end %>
ruby
1class ListingsController < ApplicationController 2 new 3 @listing = current_user.listings.build 4 end 5 6 def create 7 @listing = current_user.listings.build(listing_params) 8 9 if @listing.save 10 redirect_to listing_path(@listing), notice: "投稿を保存しました" 11 else 12 redirect_to new_listing_path, notice: "投稿を保存ができませんでした" 13 end 14 end 15 16private 17 def listing_params 18 params.require(:listing).permit(:file) 19 end 20 def set_listing 21 @listing = Listing.find(params[:id]) 22 end 23end
gemは、
gem 'paperclip', "> 5.0.0.beta1"> 2.3'
gem 'aws-sdk', '
をしようしております。
そして、現在オーディオファイルは、下図のように出力されております。
そして、showのviewのコードは下記のように書いております。
ruby
1<% if @listing.file_content_type == 'video/mp4' then %> 2 <%= video_tag @listing.file, :controls => true, width: "640", height: "360" %> 3<% elsif @listing.file_content_type == "image/jpg" || "image/jpeg" || "image/png" || "image/gif" then %> 4 <%= image_tag @listing.file, :width => 640, :height => 340 %> 5<% elsif ['audio/mpeg', 'audio/x-mpeg', 'audio/mp3', 'audio/x-mp3'].include?(@listing.file_content_type) then %> 6 <%= audio_tag @listing.file, :controls => true %> 7 <% else %> 8 <%= image_tag @listing.file %> 9 <% end %>
どのようにすれば、それぞれファイル形式に合わせ、出力できるのかが、わからない状況です。
どこを編集するば良いのでしょうか?
教えてください。
あなたの回答
tips
プレビュー