Paperclip给出NameError:未初始化的常量Model :: ImageUploader

时间:2015-01-19 00:49:27

标签: ruby-on-rails-4 rubygems paperclip

我发现了一个类似的问题: NameError: uninitialized constant Article::ImageUploader when using Carrierwave on rails 4.1.5

但我在那里尝试过这些建议: 已安装carrierwave且捆绑已完成 我已经仔细检查了我的模型。

我的用户模型有:

has_one :image, :as => :assetable, :class_name => "User::Image", :dependent => :destroy

我的用户创建工作正常,但是当我尝试访问视图时,它会被此代码破坏:

 <%= link_to image_tag((current_user.image.nil? ? Settings.default_user_image : current_user.image.attachment.url(:thumbnail)) , :style=>'max-width:60px;', :alt=>current_user.full_name, :title=>current_user.full_name), user_path(current_user) %>

我不知道为什么会破坏,早些时候它工作正常,我能想到的只是,当我的gemfile.lock发生冲突时,我再次捆绑了。

这就是我在rails console上复制问题的方法:

2.1.1 :001 > u = User.new
2.1.1 :002 > u.build_image
NameError: uninitialized constant User::User::Image
        from /home/aditya/.rvm/gems/ruby-2.1.1/gems/activerecord-4.1.0/lib/active_record/inheritance.rb:133:in `compute_type'
        from /home/aditya/.rvm/gems/ruby-2.1.1/gems/activerecord-4.1.0/lib/active_record/reflection.rb:190:in `klass'
        from /home/aditya/.rvm/gems/ruby-2.1.1/gems/activerecord-4.1.0/lib/active_record/reflection.rb:207:in `build_association'
        from /home/aditya/.rvm/gems/ruby-2.1.1/gems/activerecord-4.1.0/lib/active_record/associations/association.rb:247:in `build_record'
        from /home/aditya/.rvm/gems/ruby-2.1.1/gems/activerecord-4.1.0/lib/active_record/associations/singular_association.rb:29:in `build'
        from /home/aditya/.rvm/gems/ruby-2.1.1/gems/activerecord-4.1.0/lib/active_record/associations/builder/singular_association.rb:18:in `build_image'
        from (irb):2
        from /home/aditya/.rvm/gems/ruby-2.1.1/gems/railties-4.1.0/lib/rails/commands/console.rb:90:in `start'
        from /home/aditya/.rvm/gems/ruby-2.1.1/gems/railties-4.1.0/lib/rails/commands/console.rb:9:in `start'
        from /home/aditya/.rvm/gems/ruby-2.1.1/gems/railties-4.1.0/lib/rails/commands/commands_tasks.rb:69:in `console'
        from /home/aditya/.rvm/gems/ruby-2.1.1/gems/railties-4.1.0/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
        from /home/aditya/.rvm/gems/ruby-2.1.1/gems/railties-4.1.0/lib/rails/commands.rb:17:in `<top (required)>'
        from bin/rails:4:in `require'
        from bin/rails:4:in `<main>'

请帮忙。我现在陷入了临界点。

发生错误的文件的路径是: /demo/app/views/home/index.html.erb

图片类位于: /demo/app/models/user/image.rb

image.rb的内容

class User::Image < Asset
  has_attached_file :attachment, {
    fog_credentials: {:aws_access_key_id=>"***", :aws_secret_access_key=>"***", :provider=>"AWS"}, :fog_public=>true, :fog_directory=>"test",

    styles: { :thumbnail => "60x60#", :profile => "165x165#" },
    storage: :multiple,
    path: ":compatible_rails_root/users/files/:id/:style.:extension",
    url:  "/uploads/posts/files/:id/:style.:extension",
    multiple_if:     lambda { |user| true },
    display_from_s3: lambda { |user| true }
  }

  validates_attachment_content_type :attachment, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
end

用户模式就在 /演示/应用/模型

1 个答案:

答案 0 :(得分:0)

可能您的命名空间错误,请尝试按如下方式编写类(请注意引入的Users模块):

# app/models/users/image.rb
module Users
  class Image < Asset
    has_attached_file :attachment, { # ... 
    # ....
  end
end

并且只需修改引用Image课程中User课程的一行(唯一的变化是它现在Users::Image

# app/models/users.rb
class User < ActiveRecord::Base
  has_one :image, :as => :assetable, :class_name => "Users::Image", #...
  # ...
end

这个(可能稍后重启服务器),一切都应该没问题。