views:

53

answers:

3

If i allready have a photo in my filesystem how can i save it without submitting a form? I would like to do that in a controller when some action occurs.

I am using paperclip to save my photos. so if you have any idea how to make paperclip save it it would also be very helpful.

Paperclip:

I allready have a photo saved with paperclip. This photo belongs to a horse(model). What i want to do is when a new horse is created give that horse the same photo. so how can i do that. i tried to point to that picture with paperclip. I just copied the attributes the original photo has. it doesnt work. paperclip looks in the wrong folder.

So I have photo model. it belongs to a horse model. With paperclip the 4 attributes are needed: data_content_type, data_file_name, data_file_size, data_updated_at.

If i then create a new instance of class photo with Photo.new and give it the four paperclip parameters paperclip looks for different location that the file is saved in.

What can i do? How can i save it in controller without submitting a form? (with papeclip(preferable) or without)

thank you for any answers

A: 

You need to use some Flash (or similar tech) file uploader, as it is not possible to upload a file via AJAX. Some suggestions to look into: SWFUpload and Uploadify. You can probably dig up some Rails specific helper libs for these also on GitHub

Tanel Suurhans
It sounds like necker wants to use an existing photo that's already on the server, rather than upload a new one.
Beerlington
that is correct.
necker
+1  A: 

Have you considered using paperclip's default_url option? If you want to use the same photo by default, this might be your best option. In my application, users can upload avatars, but if they haven't yet, it shows the default image.

has_attached_file :avatar, 
                  :styles => { :thumb => "48x48#" },
                  :default_url => "/images/avatars/missing_avatar.png",
                  :default_style => :thumb
Beerlington
i do the same for avatar. but here the path to the foto i want to save is allways different. thank you for your response anyhow.
necker
+2  A: 

I am not really clear on how you did your previous attempt, but this is what I do to assign photos to a model object:

horse.photo = File.open("path/to/image/NameOfFile.extension")
horse.save!
sosborn