ruby - CarrierWave 无法将 nil 转换为 String

标签 ruby sinatra datamapper carrierwave

我只想用 carrierwave 做一个简单的图像上传,到目前为止上传工作正常(至少我是这么想的),但是当我尝试用

获取图像时
on "/entries" do

entries = Entry.all

entries.each do |entry|
  @foobar = "#{@foobar} entry.image.url"
end

"#{@foobar}"

end

我只是得到以下错误

TypeError - can't convert nil into String:
        /home/peer/.gem/ruby/1.9.1/gems/carrierwave-0.6.2/lib/carrierwave/uploader/url.rb:22:in `expand_path'
        /home/peer/.gem/ruby/1.9.1/gems/carrierwave-0.6.2/lib/carrierwave/uploader/url.rb:22:in `url'
        /home/peer/.gem/ruby/1.9.1/gems/carrierwave-0.6.2/lib/carrierwave/uploader/versions.rb:159:in `url'
        /home/peer/.gem/ruby/1.9.1/gems/carrierwave-0.6.2/lib/carrierwave/uploader/default_url.rb:8:in `url'
        /home/peer/.gem/ruby/1.9.1/gems/carrierwave-0.6.2/lib/carrierwave/uploader/url.rb:27:in `to_s'
        /home/peer/Project/myapp/controllers/view_entries.rb:4:in `block in <top (required)>'
        /home/peer/.gem/ruby/1.9.1/gems/sinatra-1.3.2/lib/sinatra/base.rb:1212:in `call'
        /home/peer/.gem/ruby/1.9.1/gems/sinatra-1.3.2/lib/sinatra/base.rb:1212:in `block in compile!'
        /home/peer/.gem/ruby/1.9.1/gems/sinatra-1.3.2/lib/sinatra/base.rb:785:in `[]'
        /home/peer/.gem/ruby/1.9.1/gems/sinatra-1.3.2/lib/sinatra/base.rb:785:in `block (3 levels) in route!'
        /home/peer/.gem/ruby/1.9.1/gems/sinatra-1.3.2/lib/sinatra/base.rb:801:in `route_eval'
        /home/peer/.gem/ruby/1.9.1/gems/sinatra-1.3.2/lib/sinatra/base.rb:785:in `block (2 levels) in route!'
        /home/peer/.gem/ruby/1.9.1/gems/sinatra-1.3.2/lib/sinatra/base.rb:822:in `block in process_route'
        /home/peer/.gem/ruby/1.9.1/gems/sinatra-1.3.2/lib/sinatra/base.rb:820:in `catch'
        /home/peer/.gem/ruby/1.9.1/gems/sinatra-1.3.2/lib/sinatra/base.rb:820:in `process_route'
        /home/peer/.gem/ruby/1.9.1/gems/sinatra-1.3.2/lib/sinatra/base.rb:784:in `block in route!'
        /home/peer/.gem/ruby/1.9.1/gems/sinatra-1.3.2/lib/sinatra/base.rb:783:in `each'
        /home/peer/.gem/ruby/1.9.1/gems/sinatra-1.3.2/lib/sinatra/base.rb:783:in `route!'
        /home/peer/.gem/ruby/1.9.1/gems/sinatra-1.3.2/lib/sinatra/base.rb:886:in `dispatch!'
        /home/peer/.gem/ruby/1.9.1/gems/sinatra-1.3.2/lib/sinatra/base.rb:719:in `block in call!'
        /home/peer/.gem/ruby/1.9.1/gems/sinatra-1.3.2/lib/sinatra/base.rb:871:in `block in invoke'
        /home/peer/.gem/ruby/1.9.1/gems/sinatra-1.3.2/lib/sinatra/base.rb:871:in `catch'
        /home/peer/.gem/ruby/1.9.1/gems/sinatra-1.3.2/lib/sinatra/base.rb:871:in `invoke'
        /home/peer/.gem/ruby/1.9.1/gems/sinatra-1.3.2/lib/sinatra/base.rb:719:in `call!'
        /home/peer/.gem/ruby/1.9.1/gems/sinatra-1.3.2/lib/sinatra/base.rb:705:in `call'
        /home/peer/.gem/ruby/1.9.1/gems/rack-protection-1.2.0/lib/rack/protection/xss_header.rb:22:in `call'
        /home/peer/.gem/ruby/1.9.1/gems/rack-protection-1.2.0/lib/rack/protection/base.rb:47:in `call'
        /home/peer/.gem/ruby/1.9.1/gems/rack-protection-1.2.0/lib/rack/protection/base.rb:47:in `call'
        /home/peer/.gem/ruby/1.9.1/gems/rack-protection-1.2.0/lib/rack/protection/path_traversal.rb:16:in `call'
        /home/peer/.gem/ruby/1.9.1/gems/rack-protection-1.2.0/lib/rack/protection/json_csrf.rb:17:in `call'
        /home/peer/.gem/ruby/1.9.1/gems/rack-protection-1.2.0/lib/rack/protection/base.rb:47:in `call'
        /home/peer/.gem/ruby/1.9.1/gems/rack-protection-1.2.0/lib/rack/protection/xss_header.rb:22:in `call'
        /home/peer/.gem/ruby/1.9.1/gems/rack-1.4.1/lib/rack/session/abstract/id.rb:205:in `context'
        /home/peer/.gem/ruby/1.9.1/gems/rack-1.4.1/lib/rack/session/abstract/id.rb:200:in `call'
        /home/peer/.gem/ruby/1.9.1/gems/rack-1.4.1/lib/rack/logger.rb:15:in `call'
        /home/peer/.gem/ruby/1.9.1/gems/rack-1.4.1/lib/rack/head.rb:9:in `call'
        /home/peer/.gem/ruby/1.9.1/gems/rack-1.4.1/lib/rack/methodoverride.rb:21:in `call'
        /home/peer/.gem/ruby/1.9.1/gems/sinatra-1.3.2/lib/sinatra/showexceptions.rb:21:in `call'
        /home/peer/.gem/ruby/1.9.1/gems/sinatra-1.3.2/lib/sinatra/base.rb:1334:in `block in call'
        /home/peer/.gem/ruby/1.9.1/gems/sinatra-1.3.2/lib/sinatra/base.rb:1416:in `synchronize'
        /home/peer/.gem/ruby/1.9.1/gems/sinatra-1.3.2/lib/sinatra/base.rb:1334:in `call'
        /home/peer/.gem/ruby/1.9.1/gems/rack-1.4.1/lib/rack/handler/webrick.rb:59:in `service'
        /usr/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
        /usr/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
        /usr/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'

这是我的入门模型:

class ImageUploader < CarrierWave::Uploader::Base
    def store_dir
        "public/user_uploads"
    end

    def extensions_white_list
        %w(jpg jpeg gif png)
    end

    storage :file
end

class Entry
    include DataMapper::Resource

    property :id, Serial
    property :text, Text
    property :created_at, String

    mount_uploader :image, ImageUploader

    belongs_to :user
end

所以我完全不知道这里出了什么问题......

为什么获取不到图片的url?

最佳答案

您应该在以下代码之前初始化@foobar:

entries.each do |entry|
  @foobar = "#{@foobar} entry.image.url"
end

请注意,@foobar 将采用最后一个条目图像 url 的值。

关于ruby - CarrierWave 无法将 nil 转换为 String,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11191596/

相关文章:

ruby - rhc 安装程序给出错误 `no such file dl/import`

heroku - Foreman:在开发和生产中使用不同的Procfile

ruby-on-rails - 为什么 DataMapper 使用混合与继承?

javascript - 在 Typo3 Extbase Controller 中使用动态 "non Domain"参数

ruby - 如何设置邮件服务器?

ruby-on-rails - Ruby on Rails 3 - 使用 View 和 Controller 创建插件或引擎?

ruby - Sinatra 以编程方式获取端口和有关服务的其他信息

javascript - sinatra + angular.js + 加载模板

链接表的 PHP Mapper 模式

html - CSS 中等效的 Ruby Truncate 方法