运行CarrierWave和Sinatra时出现未定义的方法Join-error。

时间:2015-01-23 17:43:04

标签: ruby sinatra carrierwave

我正在尝试使用网络框架Sinatra的 Gentle Introduction to CarrierWave 教程。当我运行我的应用程序时它启动很好,应用程序要求我上传文件,它没有任何问题。但是,在上传文件时,应用程序会向我抛出“#String:0x3480d50未定义的方法`连接” - 错误。

我在互联网上看了一下,我发现这个issue at github,他们说这个错误可能是由于Rack和Sinatra之间不兼容或安装了Sinatra的重复版本。

有人知道发生了什么吗?

我的uploader_app:

require 'carrierwave'
require 'sinatra'
require 'sqlite3'
require 'sequel'
require 'carrierwave/sequel'

DB = Sequel.sqlite
DB.create_table :uploads do 
    String :file    
end

# uploader
class MyUploader < CarrierWave::Uploader::Base
    storage :file
end

# model
class Upload < Sequel::Model
    mount_uploader :file, MyUploader
end

# sinatra app
get '/' do
     @uploads = Upload.all
     erb :index
end

post '/' do
    upload = Upload.new
    upload.file = params[:image]
    upload.save
    redirect to('/')
end

__END__

@@ index
<!DOCTYPE html>
<html>
    <body>
        <div>
            <form action="/" method="post" enctype="multipart/form-data">
                <input type="file" name="image" />
                <input type="submit" name="submit" value="Upload" />
            </form>
                <% @uploads.each do |upload| %>
                    <img src="<%= upload.file.url %>" />
                <% end %>
        </div>
    </body>
</html>

1 个答案:

答案 0 :(得分:2)

错误发生在this line in the Carrierwave Library

path = encode_path(file.path.gsub(File.expand_path(root), ''))

它失败,因为rootnil,因此File.expand_path(root)会引发错误。我不知道为什么root没有设置,但是下面的代码(我从this answer修改过的)对我有用:

CarrierWave.configure do |config|
  config.root = settings.root
end

我在声明Sequel类之后和定义路由之前将它添加到代码中。可能最好将其粘贴在configure block too中。请注意,上面代码中的settings.rootSinatra's root setting

这似乎不是由Rack 1.6.0和Sinatra 1.4.5之间的当前问题引起的,因为我正在运行的,尽管我在Ruby v2.1.2上作为我在上面的评论中提到过。

根据您的需要,Sinatra的root可能不是放置东西的最佳位置,因为我最终在项目根目录中找到了一个名为&#34; uploads&#34;其中包含文件,但config.root显然需要设置为某事

希望有所帮助。