Rails + CarrierWave: NoMethodError: undefined method `name' for nil:NilClass

后端 未结 6 505
挽巷
挽巷 2021-01-18 02:19

I am using CarrierWave with Rails 3.1. I am getting the following error message when I submit the form (trying to upload an image):

Error Message:

Ac         


        
相关标签:
6条回答
  • 2021-01-18 02:36

    Make sure you use - mount_uploader :image, ImageUploader in your model like here -

    class CarImage < ActiveRecord::Base 
      belongs_to :car
      mount_uploader :image, ImageUploader
    end
    

    Regards

    Robbie

    0 讨论(0)
  • 2021-01-18 02:37

    I came across this post because I had the same error you described, but restarting the server didn't resolve it (as suggested by other answers). In my case, the problem was caused because I used mount_uploader before attr_accessible. By switching them I solved the problem.

    0 讨论(0)
  • 2021-01-18 02:40

    I had a similar problem. Solution was changing:

    attr_accessible :title, :body, :user_id, :draft, :post_type, :image
    belongs_to :user
    mount_uploader :image_url, ImageUploader
    

    to:

    attr_accessible :title, :body, :user_id, :draft, :post_type, :image_url
    belongs_to :user
    mount_uploader :image_url, ImageUploader
    
    0 讨论(0)
  • 2021-01-18 02:45

    Your image uploader class was not loaded into your current rails server thread. Reload rails server and it should work fine =).

    0 讨论(0)
  • 2021-01-18 02:49

    Make sure in your model:

    mount_uploader :image, ImageLoader
    

    Remember that :image must be the string/text type.

    0 讨论(0)
  • 2021-01-18 02:52

    I experienced the some problem and the cause is (probably always) that the UploadedFile object has been sent to the attribute carrierwave was mounted on. The db adapter cannot serialize this object and will therefore throw this error.

    Make sure that:

    • the uploader has been properly mounted
    • you don't use write_attribute to write the uploaded file (which was the cause of my problem). Use the accessor instead: model.send('image=', params[:model][:image]). Uglier, but better.
    0 讨论(0)
提交回复
热议问题