Foreword: I'm a REAL rails newbie. I'm developing my first web application with it and therefore even basic concepts are hard to understand for me.
Having said thay, my problem:
I'm planning to use Paperclip (and Paperclip only) to store pdfs in my applications (since I'm expecting the pdfs to be around 0.5mb). The tutorial on Paperclip's github didn't one thing clear for me:
<%= f.label :pdf %>
<%= f.file_field :pdf %>
<td><%= (link_to 'Related file', task.pdf.url, :target => "_blank") if task.pdf.exists? %></td>
Your migration to add the pdf attachment to a table (In this example I am adding the attachment PDF to a
Documents table) should look like this:
class AddAttachmentPdfToDocuments < ActiveRecord::Migration def self.up change_table :documents do |t| t.attachment :pdf end end def self.down remove_attachment :documents, :pdf end end
You need to run
rake db:migrate after you create this migration so that the pdf column actually gets added to the table.
Your Model code for paperclip (Again, I'm using A
documents model for example) should look something like this:
has_attached_file :pdf, :use_timestamp => false validates_attachment_content_type :pdf, :content_type => ['application/pdf', 'text/plain']
To add a pdf, you need to add an input to your form, for example this is the loop to use to ask for a pdf attachment:
<%= form_for @document do |f| %> <%= f.input :title, label: "Title" %> <%= f.input :pdf, label: "Upload document:" %> <%= f.button :submit %> <% end %>
In your controler (again using
documents as the controler) you need to pass the params
def documents_params params.require(:document).permit(:title, :pdf) end
Note, I am using
document as my controller, model, form etc, so if you are using a different name you need to change that.
If you have problems leave a comment and I will try to help