Copy a Paperclip image to a new record in Rails 4

前提是你 提交于 2019-12-02 01:43:50

For the first part, you could pull the attributes individually and set them to the instance variable, and for the second part, you could use URI.

def copy
  @source = Pin.find(params[:id])
  @image = URI.parse(@source.image)
  @pin = Pin.new(artist: @source.artist, title: @source.title, year: @source.year, image: @image)
  render 'new'
end

Here is what I ended up doing to copy the image to a new pin form.

I have Paperclip's image_remote_url functionality set up already, but I needed to add .to_s to the method below in the pin.rb:

def image_remote_url=(url_value)
 self.image = URI.parse(url_value).to_s unless url_value.blank?
 super
end

Then my controller:

def copy
 @source = Pin.find(params[:id])
 @image = @source.image.url
 @pin = Pin.new(artist: @source.artist, album: @source.album, year: @source.year, image_remote_url: @image)
 render 'new'
end

This pastes the url of the image into the new form. It only works on my Heroku production version, not the local version.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!