gpt4 book ai didi

ruby - 运行 CarrierWave 和 Sinatra 时未定义的方法连接错误。

转载 作者:太空宇宙 更新时间:2023-11-03 15:59:43 25 4
gpt4 key购买 nike

我正在尝试 Gentle Introduction to CarrierWave -使用网络框架 Sinatra 的教程。当我运行我的应用程序时,它启动得很好并且应用程序要求我上传文件并且它没有任何问题。然而,在上传文件时,应用程序向我抛出一个“#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>

最佳答案

错误发生在 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 可能不是放置内容的最佳位置,因为我最终在项目根目录中找到了一个名为“uploads”的目录,其中包含文件,但是config.root 显然需要设置为某物

希望对您有所帮助。

关于ruby - 运行 CarrierWave 和 Sinatra 时未定义的方法连接错误。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28115650/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com