I'm a total Rails newbie so apologies if this is a simple question, but I've been unable to find a clear answer. I'm trying to allow a user to attach an image to a post either by uploading a file or entering the URL of an image using the Paperclip gem.
File upload works fine, but using the URL method I receive a 'No handler defined' error
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
I must confess that I don't fully understand what is happening at each stage of the process which is why I'm struggling to debug it.
I am recommending carrierwave gem over paperclip. Easier to parse from link.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.