rails勉強してます3

よくあるファイルアクセス権限系の話しでアップロードされたpdfをDB保存 & embedタグでDBに保存したpdfを表示

migration

$ rails g model pdf name:string data:binary

$ rails db:migrate

controller

#html表示
def template
  @pdf = Pdf.new
end

#DB保存したバイナリを返す
def show
  @pdf = Pdf.last
  if @pdf.nil?
    send_data(@pdf.data, :disposition => "inline", :type => "application/pdf")
  end
end

#バイナリデータの保存
def create
  @pdf.name = params[:pdf][:data].original_filename
  @pdf.data = params[:pdf][:data].read
  @pdf.save
  render 'template'
end

view

<embed src="<%= show_path %>" width="425" height="400"></embed>
<%= form_for(@pdf, url:create_path, method: :post) do |f| %>
<%= f.file_field :data %>
<%= f.submit "画像を投稿する" %>
<% end %>

validateはまだ(^_^;)