2

私は単にキャリアウェーブで画像をアップロードしたいだけで、アップロードはこれまでのところうまくいきました(少なくともそれは私が思うことです)が、画像を取得しようとすると

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を取得できないのはなぜですか?

4

2 に答える 2

1

次のコードの前に @foobar を初期化する必要があります。

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

@foobar は最後のエントリ画像の URL の値を取ることに注意してください。

于 2012-06-25T15:01:31.690 に答える
0

次のように、Carrierwave のルート パスを設定する必要がある場合があります。

CarrierWave.configure do |config|
  config.root = Rails.root
end
于 2017-04-21T02:41:25.167 に答える