Sinatra/Carrierwave:如何在使用现有图像的同时更新记录?

发布于 2024-12-09 02:50:27 字数 1620 浏览 0 评论 0原文

我的超级简单的 Sinatra 应用程序是一个笔记列表,每个笔记都有一个附加图像。

我设置了一个“放置”路线,可以让我更新笔记,但除非我重新上传图像,否则我会丢失它(当我提交表单时,note.image 设置为“nil”)。

预先感谢您的帮助!

这是我的上传者:

class MyUploader < CarrierWave::Uploader::Base
  include CarrierWave::MimeTypes
  process :set_content_type
  storage :fog
end

这是我的 Note 类:

class Note
    include DataMapper::Resource
    property :id, Serial
    property :image, String, :auto_validation => false
    property :content, Text, :required => true
    property :created_at, Date
    property :updated_at, Date
    mount_uploader :image, MyUploader                   
end

这是我的“放置”路线:

put '/:id' do
  n = Note.get params[:id]
    unless n
      redirect '/', :error => "Can't find that note."
    end
  n.image = params[:image] 
  n.credit = params[:content]
  n.date = params[:date]
  n.updated_at = Time.now
  if n.save
    redirect '/', :notice => 'Note updated successfully.'
  else
    redirect '/', :error => 'Error updating note.'
  end
end

这是我用来更新笔记的表单:

<% if @note %>
  <form action="/<%= @note.id %>" method="post" id="edit" enctype="multipart/form-data">
    <input type="hidden" name="_method" value="put">
    <p><input type="text" name="credit" value="<%=h @note.content %>"></p>
    <p><input type="file" name="image" /></p>
    <br><br>
    <input type="submit">
  </form>
<% else %>
    <p>Note not found.</p>
<% end %>

My super-simple Sinatra app is a list of notes, and each note has an attached image.

I've got a 'put' route set up that lets me update notes, but unless I re-upload the image, I lose it (note.image is set to 'nil' when I submit the form).

Thanks in advance for your help!

Here's my uploader:

class MyUploader < CarrierWave::Uploader::Base
  include CarrierWave::MimeTypes
  process :set_content_type
  storage :fog
end

Here's my Note class:

class Note
    include DataMapper::Resource
    property :id, Serial
    property :image, String, :auto_validation => false
    property :content, Text, :required => true
    property :created_at, Date
    property :updated_at, Date
    mount_uploader :image, MyUploader                   
end

Here's my 'put' route:

put '/:id' do
  n = Note.get params[:id]
    unless n
      redirect '/', :error => "Can't find that note."
    end
  n.image = params[:image] 
  n.credit = params[:content]
  n.date = params[:date]
  n.updated_at = Time.now
  if n.save
    redirect '/', :notice => 'Note updated successfully.'
  else
    redirect '/', :error => 'Error updating note.'
  end
end

And here's the form I'm using to update notes:

<% if @note %>
  <form action="/<%= @note.id %>" method="post" id="edit" enctype="multipart/form-data">
    <input type="hidden" name="_method" value="put">
    <p><input type="text" name="credit" value="<%=h @note.content %>"></p>
    <p><input type="file" name="image" /></p>
    <br><br>
    <input type="submit">
  </form>
<% else %>
    <p>Note not found.</p>
<% end %>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

不奢求什么 2024-12-16 02:50:27

一个简单的 if 检查就是您所需要的:如果 params[image] 为 nil,则跳过 n.image = params[:image]

n.image = params[:image] if params[:image]

我使用类似的方法来创建自定义当我使用包含 CarrierWave 图像的 ActiveRecord 模型时,Rails 验证检查。检查 n.image 是否也不是 nil 可能不是一个坏主意 - 如果它是 nil 我想应该强制上传图像。

A simple if check is what you need: if params[image] is nil you skip the n.image = params[:image]

n.image = params[:image] if params[:image]

I use similar approach to create a custom Rails validation check when I work with ActiveRecord models that contain CarrierWave images. Probably it won't be a bad idea to check whether or not n.image isn't nil as well - if it's nil I guess it should be mandatory to upload an image.

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