简体   繁体   中英

undefined method 'has_attached_file' with paperclip on Rails 5

Rails server will not start after install of paperclip. I have this error message in the console:

undefined method 'has_attached_file'.

In my Gemfile

gem "paperclip", :git => "http://github.com/thoughtbot/paperclip.git"

I tried this in my config/environments/development.rb

config.gem "paperclip"

and this in my config/application.rb

Paperclip::Railtie.insert

I have ImageMagik installed on my computer (I'm on windows 10). Any clue ?

I had a message in the console after install of paperclip, saying that paperclip is now compatible with aws-sdk >= 2.0.0. So I also set the latest aws-sdk-ruby from github...

EDIT : also tried this after getting the path with the command 'which convert'

Paperclip.options[:command_path] = "/c/Program Files/ImageMagick-7.0.7-Q16/convert"

First thing I'd change is to use a version number instead of getting the package directly from GitHub.

in Gemfile :

Change

gem "paperclip", :git => "http://github.com/thoughtbot/paperclip.git"

To

gem "paperclip", "~> 6.0"

Make sure to run the rails generate paperclip command (example: rails generate paperclip photo image ) to add the needed attachment field to your database model schema.

Once you have the attachment field you can use has_attached_file to mount paperclip to that field (example: has_attached_file :image ).

And don't forget to restart your server.

PS: There's no need to use config.gem "paperclip" and Paperclip::Railtie.insert , I wasn't able to find any mention of them in the GitHub Paperclip documentation so I'm sure they are discontinued now as they were used in pre Rails 4 applications.

PPS: I would strongly recommend you dual boot to a Unix operating system (be it a Linux distribution or macOS) or use an online IDE such as Cloud9 instead of using Windows. It's simply a bad idea and while working on your project you want to replicate to the smallest detail your production environment.

Try to add

include Paperclip::Glue

to your Model.

I'm all set now. Apparently there was a problem with the database construction. For the rest, everything is well explained on github https://github.com/thoughtbot/paperclip but 1) I was following a tutorial with a few steps missing and 2) there was a specific problem for windows.

Dropping all the tables and migrating again from scratch seems to have solved the very basic problem which caused the error.

Windows users, pay attention to your path setting of the file.exe which has nothing to do with ImageMagik. You can place this file.exe anywhere you want, in my case

Paperclip.options[:command_path] = 'C:\Sites\utils\GnuWin32\bin'

Thanks anyway!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM