将文件上传添加到 Sinatra 教程
编辑 - “载波”不适用于 Sinatra 1.3。 Sinatra 1.2.7 震撼了这段代码!
我正在学习 Ruby,刚刚完成了这个很棒的 Sinatra 教程: http://net.tutsplus.com/tutorials/ruby/ sing-with-sinatra-the-encore/
完整的工作代码在这里(没有 Bundler,所以需要安装几个 gem) http://nettuts.s3.amazonaws.com/953_sinatra3/Source.zip
我感觉很好,我想了解更多!我为自己设定的下一个挑战是向该教程添加文件上传功能,但我被难住了。我想使用 Carrierwave,并尝试将其集成到已完成的教程中。
首先,我需要“载波”和“载波数据映射器”:
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)
但是,当然,如果我不需要“载波”,当 MyUploader 尝试继承它时,我会收到错误...
提前感谢您的任何提示。我感觉这里很近,却又很远!
EDIT - 'carrierwave' doesn't work with Sinatra 1.3.
Sinatra 1.2.7 rocks this code!
I'm learning Ruby, and just finished this awesome Sinatra tutorial:
http://net.tutsplus.com/tutorials/ruby/singing-with-sinatra-the-encore/
The complete, working code is here (no Bundler, so requires a couple gems to be installed)
http://nettuts.s3.amazonaws.com/953_sinatra3/Source.zip
I feel good, I want to learn more! The next challenge I've set for myself is to add file uploading capability to that tutorial, and I'm stumped. I want to use Carrierwave, and am attempting to integrate it into the completed tutorial.
First, I'm requiring 'carrierwave' and 'carrierwave-datamapper':
require 'carrierwave'
require 'carrierwave/datamapper'
Then I'm creating a new class:
class MyUploader < CarrierWave::Uploader::Base #via a Carrierwave tutorial
storage :file
end
Adding to the Notes class:
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
Adding to post:
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
Lastly, I'm adding uploading to the form:
<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>
I'm getting this error:
/gems/carrierwave-0.5.7/lib/carrierwave.rb:107:in `<top (required)>': private method `public' called for Sinatra::Application:Class (NoMethodError)
But of course if I don't require 'carrierwave', I get an error when MyUploader tries to inherit from it...
Thanks in advance for any tips. I feel so close here, and yet so far!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此错误看起来是由 sinatra 最近的更改引起的。这是版本 1.3 的变更日志:
检查是否有更新版本的 Carrierwave 或使用旧版本的 sinatra。
This error looks like it's caused by a recent change to sinatra. This is in the changelog for version 1.3:
Either check if there's a more recent version of carrierwave or use a previous version of sinatra.