将文件上传添加到 Sinatra 教程

发布于 2024-12-08 02:20:06 字数 2299 浏览 0 评论 0原文

编辑 - “载波”不适用于 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&hellip;"></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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酷到爆炸 2024-12-15 02:20:06

此错误看起来是由 sinatra 最近的更改引起的。这是版本 1.3 的变更日志

:public 重命名为 :public_folder 以避免覆盖 Ruby 的内置 public 方法/关键字。 set(:public, ...) 仍然可行,但会显示警告。 (康斯坦丁·哈斯)

检查是否有更新版本的 Carrierwave 或使用旧版本的 sinatra。

This error looks like it's caused by a recent change to sinatra. This is in the changelog for version 1.3:

Renamed :public to :public_folder to avoid overriding Ruby's built-in public method/keyword. set(:public, ...) is still possible but shows a warning. (Konstantin Haase)

Either check if there's a more recent version of carrierwave or use a previous version of sinatra.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文