Paperclip Error: model missing required attr_accessor for 'avatar_file_name'

前端 未结 3 2029
陌清茗
陌清茗 2020-12-18 21:08

I then want to use Paperclip to have photos for each Listing. I added the appropriate code to the listings show.html.erb, the listing.rb model, the listings_controller.rb an

相关标签:
3条回答
  • 2020-12-18 21:19

    From the error message, file_name is not available in the model to which you're trying to save. I had a similar problem and realized I forgot to run the Paperclip migration:

    rails generate paperclip [Model Name] [Attachment] (e.g., rails g paperclip Images image)

    If that doesn't work, since the issue it's having is column "file_name", try adding that to the model (e.g. rails g migration addFilenameToImages file_name:string)

    This worked for me, so hopefully it helps some of you too!

    0 讨论(0)
  • 2020-12-18 21:20

    Make sure that in paperclip_database:migration you use plural for the new table name and in the paperclip generator you use singular:

    rails g paperclip_database:migration cms_article_category cms_article_category_images
    rails g paperclip cms_article_category cms_article_category_image
    

    And check the resulting column names in your database In your example the column in the avatar table should be called avatar_file_name

    0 讨论(0)
  • 2020-12-18 21:22

    I suppose you forgot to create the corresponding fields for avatar in listings table.

    I would suggest you to generate a migration to add avatar to listings table as below:

    rails generate paperclip listing avatar
    

    Then run rake db:migrate

    UPDATE

    As per your comments and EDIT, you have a migration file to add avatar to listings table which you created by running rails generate paperclip user avatar but unfortunately for some reason its not going through i.e., there are no avatar specific fields("avatar_file_name", "avatar_content_type", "avatar_file_size" and "avatar_updated_at") in listings table as per your db/schema.rb. This is a very strange behavior.

    I would suggest you to follow the below mentioned steps in order:

    Destroy the existing migration, if any:

    rails destroy paperclip listing avatar  
    

    Generate a new migration:

    rails generate paperclip listing avatar
    

    Run

    rake db:migrate
    

    UPDATE 2

    I hope you did not down voted me (but someone did), so I would like to bring it to notice that it is an ongoing issue with Paperclip and I did suggest a solution in my comments (Mar 31) as below:

    I want you to try as gem 'paperclip', :git => "git://github.com/thoughtbot/paperclip.git" in Gemfile and then bundle install. Let me know when you finish

    Apparently it wasn't noticed by you or someone who down voted me today. Also, you said No errors as far as I can tell, image here: i.imgur.com/c8KGTa3.png BUT if you look at the output there is an error stating clearly:

    migration_file_name': protected methodmigration_file_name' called for PaperclipGenerator:0x007fb3c6494c20 (NoMethodError)

    0 讨论(0)
提交回复
热议问题