未初始化的常量 - 在Rails中完全丢失了

时间:2014-06-06 20:20:17

标签: ruby-on-rails ruby ruby-on-rails-4 initialization carrierwave

我正在尝试将文件上传到包含Carrierwave的表单,但每次运行我的rails服务器时,都会收到以下消息:

ListingsController #index

中的NameError

未初始化的常量清单:: ImageUpLoader

提取的来源(第2行附近):

 1.    class Listings < ActiveRecord::Base
 2.         mount_uploader :image, ImageUpLoader
 3.   end

Rails.root:C:/ Sites / redemo

这是我的错误日志所说的

Started GET "/listings/1/edit" for 127.0.0.1 at 2014-06-06 20:09:16 +0100
Processing by ListingsController#edit as HTML
Parameters: {"id"=>"1"}
[1m[36mListing Load (0.0ms)[0m  [1mSELECT "listings".* FROM "listings" WHERE        "listings"."id" = ? LIMIT 1[0m  [["id", "1"]]
Rendered listings/_form.html.erb (111.0ms)
Rendered listings/edit.html.erb within layouts/application (121.0ms)
Rendered layouts/_header.html.erb (0.0ms)
Rendered layouts/_footer.html.erb (0.0ms)
Completed 200 OK in 248ms (Views: 240.0ms | ActiveRecord: 0.0ms)


Started PATCH "/listings/1" for 127.0.0.1 at 2014-06-06 20:09:20 +0100
Processing by ListingsController#update as HTML
Parameters: {"utf8"=>"✓",    k

[1m [35m(0.0ms)[0m回滚交易           在19毫秒内完成500内部服务器错误

ArgumentError (wrong number of arguments (2 for 1)):
  app/controllers/listings_controller.rb:45:in `block in update'
  app/controllers/listings_controller.rb:44:in `update'


Rendered C:/Ruby200/lib/ruby/gems/2.0.0/gems/actionpack-   4.0.0/lib/action_dispatch/middleware/templates/rescues/_source.erb (2.0ms)
Rendered C:/Ruby200/lib/ruby/gems/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.erb (3.0ms)
Rendered C:/Ruby200/lib/ruby/gems/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (2.0ms)
Rendered C:/Ruby200/lib/ruby/gems/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (146.0ms)

我完全失去了这个。有什么建议吗?

3 个答案:

答案 0 :(得分:2)

逻辑上,您必须定义CarrierWave Uploader类,如下所示:

class ImageUploader < CarrierWave::Uploader::Base
  ## ....
end

注意 小写ImageUploader的班级名称l

但是您将上传者安装为ImageUpLoader注意L资本),导致错误uninitialized constant Listings::ImageUpLoader

更新代码如下:

class Listings < ActiveRecord::Base
  mount_uploader :image, ImageUploader ## ImageUploader with "l" in lowercase
end

答案 1 :(得分:0)

我认为应该是;

class Listings < ActiveRecord::Base
   mount_uploader :image, ImageUploader
end

答案 2 :(得分:0)

我认为有两个问题: 1) 查看控制器的更新方法:app / controllers / listings_controller.rb(第44-45行)。

检查参数并确保您传递了预期的参数。

2)未初始化的常量清单:: ImageUpLoader:检查模型中的名称,并将其与控制器的索引方法参考进行比较。