Tony Tambe
Tony Tambe

Reputation: 573

Copy a Paperclip image to a new record in Rails 4

My site is for posting album reviews, which are called Pins. The pins model has the following attributes:

:artist, :year, :title, :rank, :description, and :image

The image uses Paperclip and is stored on Amazon S3 if that matters

I am trying to allow a user to see a review that another user posted and click a link to more simply write their own review for that same album. So basically the link takes them to the Pin.new page and the form already has the :artist, :title, :image, and :year filled in.

I figured out how to do it while bringing ALL of the attributes, including the :description and :rank, which I don't want. I am also not able to bring over the image to the new form.

Here is the pins_controller.rb code I'm using:

  def copy
    @source = Pin.find(params[:id])
    @pin = @source.dup
    render 'new'
  end

And in my show view:

<%= link_to "copy", copy_pin_path(params[:id]) %> 

So question one is how to @source.dup only :artist, :title, and :year

question two is how to bring over the paperclip image. I tried adding this to my pins_controller.rb:

  def copy
    @source = Pin.find(params[:id])
    @pin = @source.dup
    @pin.image = @source.image
    render 'new'
  end

but that didn't work.

UPDATE: miller350 answered my first question, but I still can't get the paperclip image to copy over to the "new" form. In the console I can copy from one Pin record and save a new pin like this:

r = Pin.new
r.image = Pin.find(83).image
r.save

I think the issue is that the new pin form requires that an image is chosen from the user's PC before saving the pin. Whatever that function of Paperclip is, I think that is where I'm getting stuck.

Upvotes: 3

Views: 2050

Answers (3)

Gaurav Patil
Gaurav Patil

Reputation: 1382

Try below code to copy paperclip image from one record to another Suppose you have one active record of model Course and copy image to another record

c1 = Course.last
c2.image = c1.image
c2.save

how it works - c2.image = c1.image - It copy from c1.image to local file and when you save it then it first delete c2.image if present and set new image to that record

Upvotes: 1

Tony Tambe
Tony Tambe

Reputation: 573

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.

Upvotes: 1

miler350
miler350

Reputation: 1421

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

Upvotes: 2

Related Questions