【问题标题】:Undefined Method Join-error when running CarrierWave and Sinatra.运行 CarrierWave 和 Sinatra 时出现未定义的方法加入错误。
【发布时间】:2015-01-23 17:43:04
【问题描述】:

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

我在互联网上浏览了一下,发现了这个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>

【问题讨论】:

  • 您能与我们分享堆栈跟踪吗?
  • @iain Sure link 它实际上给了我两个错误:一个如上所述的 NoMethodError 和一个“TypeError:无法将 nil 转换为字符串”。非常感谢任何帮助。
  • 你不会发疯的。我刚刚安装并运行它,得到了同样的错误。我正在运行 Ruby 2.1.2 并使用捆绑程序对 gem 进行沙盒处理,但仍然失败。我会看看我能找到什么。

标签: ruby sinatra carrierwave


【解决方案1】:

错误发生在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 之间的当前问题引起的,因为这是我正在运行的,尽管我在上面的 cmets 中提到的 Ruby v2.1.2 上。

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

希望对您有所帮助。

【讨论】:

  • 我认为与 Rack 1.6.0 – Sinatra 1.4.5 问题有关。该问题出在异常处理程序中,因此如果您修复原始错误(就像您在此处所做的那样),undefined method join 错误将不会出现并掩盖原始错误。
  • @matt 你说得对,我忘了问题最初是关于连接错误而不是类型错误(尽管它真的应该是这样的:)
  • 就是这样。谢谢!
猜你喜欢
  • 1970-01-01
  • 2015-06-02
  • 2013-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-18
相关资源
最近更新 更多