【发布时间】:2018-02-25 17:28:22
【问题描述】:
我完全是 Rails 新手,如果这是一个简单的问题,我深表歉意,但我一直无法找到明确的答案。我正在尝试允许用户通过上传文件或使用 Paperclip gem 输入图像的 URL 来将图像附加到帖子中。
文件上传工作正常,但使用 URL 方法我收到 'No handler defined' 错误
link.rb
require "open-uri"
class Link < ApplicationRecord
attr_reader :image_from_url
has_attached_file :image, styles: { medium: "600x600>", thumb: "100x100#" }, default_url: "/images/:style/missing.png"
validates_attachment_content_type :image, content_type: /\Aimage\/.*\z/
def image_from_url=(url)
self.image = URI.parse(url)
@image_from_url = url
end
end
_form.html.erb
<%= form_with(model: link, local: true) do |form| %>
<%= form.file_field :image, id: :link_image %>
<%= form.text_field :image, id: :link_image_url %>
<% end %>
links_controller.erb
def create
@link = current_user.links.build(link_params)
end
def link_params
params.require(:link).permit(:title, :url, :description, :image, :slug)
end
我必须承认,我并不完全了解过程的每个阶段发生了什么,这就是我努力调试它的原因。
【问题讨论】:
-
据我了解:当表单提交时,它会调用模型中的 image_from_url 函数,然后解析url并保存在图像列下?表单提交但它没有保存图像,我在日志中没有收到任何错误
标签: ruby-on-rails ruby-on-rails-5 paperclip