问题
So, I am building an application where users can create a Pin and upload pictures and PDF/Word.docs to the Pin. It seems like I have done everything to enable those options;
Created methods in the Pins model:
class Pin < ActiveRecord::Base
belongs_to :user
has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }
validates_attachment :image, content_type: { content_type: ["image/jpg", "image/jpeg", "image/png", "image/gif"] }
validates :image, presence: true
has_attached_file :document
validates_attachment :document, :content_type => { :content_type => %w(application/pdf application/msword application/vnd.openxmlformats-officedocument.wordprocessingml.document) }
end
Generated migration in the DB
class AddFileToPin < ActiveRecord::Migration
def self.up
change_table :pins do |t|
t.attachment :document
end
end
def self.down
drop_attached_file :pins, :document
end
end
Then ran rake db:migrate
to add the few lines for the documents in my schema
Now here is the issue (or issues):
I ran rails console
in my terminal and started to take a look at some of the Pins I've created.
ex: Pin.find(3)
(looking up the 3rd Pin I created, with an image and a PDF attached to it)
Here is the output:
Pin Load (0.5ms) SELECT "pins".* FROM "pins" WHERE "pins"."id" = ? LIMIT 1 [["id", 3]]
=> #<Pin id: 3, description: "pdf test 2", created_at: "2001-01-01 00:05:13", updated_at: "2001-01-01 00:05:13", user_id: 1, image_file_name: "Screen_Shot_2014-06-25_at_11.45.55_PM.png", image_content_type: "image/png", image_file_size: 45789, image_updated_at: "2001-01-01 00:05:11", document_file_name: nil, document_content_type: nil, document_file_size: nil, document_updated_at: nil>
As you can see the PDF's content is "nil". So, I am wondering if it was truly saved or not and if not, how can I solve this problem?
来源:https://stackoverflow.com/questions/24740316/get-nil-after-uploading-pdf-worddocs-to-application-rails