我只是想用载波进行简单的图片上传,到目前为止上传作品(至少这是我的想法),但当我试图用
获取图像时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
所以我绝对不知道这里有什么问题......
为什么无法获取图片的网址?
答案 0 :(得分:1)
您应该在以下代码之前初始化@foobar:
entries.each do |entry|
@foobar = "#{@foobar} entry.image.url"
end
请注意@foobar将获取最后一个条目图片网址的值。
答案 1 :(得分:0)
您可能需要设置Carrierwave的根路径,例如:
CarrierWave.configure do |config|
config.root = Rails.root
end