model missing required attr_accessor for 'photo_file_name' when uploading with paperclip and S3 on heroku

自闭症网瘾萝莉.ら 提交于 2019-12-17 19:17:01

问题


Setting up paperclip with S3 in my linux dev environment was a snap -- everything works out of the box. However, I can't get it to work on Heroku.

When I try to do an upload, the log shows:

Processing ItemsController#create (for 72.177.97.9 at 2010-08-26 16:35:14) [POST]  
  Parameters: {"commit"=>"Create", "authenticity_token"=>"0Hy3qvQBHE1gvFVaq32HMy2ZIopelV0BHbrSeHkO1Qw=", "item"=>{"photo"=>#<File:/home/slugs/270862_4aa601b_4b6f/mnt/tmp/RackMultipart20100826-6286-1256pvc-0>, "price"=>"342", "name"=>"a new item", "description"=>"a new item", "sold"=>"0"}}

Paperclip::PaperclipError (Item model missing required attr_accessor for 'photo_file_name'):

I found one blog post that referenced this error, and it said to add this to my model:

attr_accessor :photo_file_name
attr_accessor :photo_content_type
attr_accessor :photo_file_size
attr_accessor :photo_updated_at

That does indeed make the model missing required attr_accessor for 'photo_file_name' error go away, but it still doesn't work. See my other question for details. As I have figured out that with the attr_accessor lines added to my model the uploads fail even on my dev system, I suspect that is not the right answer.


回答1:


Found the problem: needed to update the database.

heroku run rake:db:migrate

heroku restart

I had done what I thought would have accomplished the same thing already:

heroku rake db:schema:load

but perhaps that doesn't work or something went wrong in the process.




回答2:


Error like this occurs if you create wrong column type in migration. When you define new table migration for paperclip, you need to specify t.attachment :name insted of t.string :name. Or add_attachment :table, :name when you add new paperclip column in existed table. And now you don't need to add these attributes in attr_accessor in model.




回答3:


Well, this message seems to be because the columns it's missing. Try create a migration creating the columns:

class AddPhotoToEvent < ActiveRecord::Migration
  def change
    add_column :events, :photo_file_name,    :string
    add_column :events, :photo_content_type, :string
    add_column :events, :photo_file_size,    :integer
    add_column :events, :photo_updated_at,   :datetime
  end

end

This work for me, here i have a table events with photo



来源:https://stackoverflow.com/questions/3580592/model-missing-required-attr-accessor-for-photo-file-name-when-uploading-with-p

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!