mardi 29 mai 2018

how to send file's binary to params and save it in DB with paperclip_database

Sorry for maybe newbie question. I want to use a paperclip and paperclip_database gems to attach and save files in my database. But I stuck on sending file's data from views to controller. I've done all from this, and this resources. As the result I have next models:

class User < ActiveRecord::Base
  has_many :certificates, :dependent => :destroy
  accepts_nested_attributes_for :certificates, :allow_destroy => true
end
class Certificate < ActiveRecord::Base
  belongs_to :user

  attr_accessor :image
  has_attached_file :image,
                    :storage => :database,
                    :database_table => 'image_files',
                    :cascade_deletion => true
end

in a controller

Users_controller
  def new
    @user = User.new
    @user.certificates.build
    ~~~~~~~~~~~~~~~~
    ~~~~~~~~~~~~~~~~
  end

  def create
    @user = User.new(params[:user])
    ~~~~~~~~~~~~~~~~
    ~~~~~~~~~~~~~~~~
  end
end

and my view form is:

<%= form_tag @user, method: :put, :html => { :multipart => true } do |f|%>
  <%= fields_for each_event_entry.certificates.first do |c| %>
    <tr>
      <th>Certificate</th>
      <td>
        <%= c.file_field :image %>
      </td>
    </tr>
  <% end %>
<% end %>

But when I attach a file and try to submit, I've only got a file name in params:

{
 "user" => {"some"=>"params"}
 "certificate"=>{"image"=>"IMG_1642.JPG"}
 }

And certificate is saved whithout attached file. Any help will be extremely appreciated.

Aucun commentaire:

Enregistrer un commentaire