编辑 - 'carrierwave'不适用于Sinatra 1.3。 Sinatra 1.2.7晃动了这段代码!
我正在学习Ruby,刚刚完成了这个很棒的Sinatra教程: http://net.tutsplus.com/tutorials/ruby/singing-with-sinatra-the-encore/
这里有完整的工作代码(没有Bundler,因此需要安装几个宝石) http://nettuts.s3.amazonaws.com/953_sinatra3/Source.zip
我感觉很好,我想了解更多!我为自己设定的下一个挑战是为该教程添加文件上传功能,我很难过。我想使用Carrierwave,并尝试将其集成到已完成的教程中。
首先,我要求'carrierwave'和'carrierwave-datamapper':
require 'carrierwave'
require 'carrierwave/datamapper'
然后我正在创建一个新类:
class MyUploader < CarrierWave::Uploader::Base #via a Carrierwave tutorial
storage :file
end
添加到Notes类:
class Note
include DataMapper::Resource
property :id, Serial
property :content, Text, :required => true
property :complete, Boolean, :required => true, :default => 0
property :created_at, DateTime
property :updated_at, DateTime
property :image, String, :auto_validation => false # trying to add image uploading
mount_uploader :image, MyUploader # trying to add image uploading
end
添加到帖子:
post '/' do
n = Note.new
n.content = params[:content]
n.image = params[:image] # trying to add image uploading
n.created_at = Time.now
n.updated_at = Time.now
n.upload =
if n.save
redirect '/', :notice => 'Note created successfully.'
else
redirect '/', :error => 'Failed to save note.'
end
end
最后,我正在向表单添加上传内容:
<section id="add">
<form action="/" method="post" enctype="multipart/form-data">
<textarea name="content" placeholder="Your note…"></textarea>
<p><input type="file" name="image" /></p>
<input type="submit" value="Take Note!">
</form>
</section>
我收到了这个错误:
/gems/carrierwave-0.5.7/lib/carrierwave.rb:107:in `<top (required)>': private method `public' called for Sinatra::Application:Class (NoMethodError)
但是当然如果我不需要'carrierwave',当MyUploader尝试从它继承时我会收到错误...
提前感谢任何提示。我觉得这里很近,但到目前为止!
答案 0 :(得分:2)
这个错误看起来像是最近对sinatra的改变造成的。这是changelog for version 1.3:
将
:public
重命名为:public_folder
,以避免覆盖Ruby的内置public
方法/关键字。set(:public, ...)
仍有可能,但会显示警告。 (Konstantin Haase)
检查是否有更新版本的carrierwave或使用以前版本的sinatra。