I am using the two gems rails3_acts_as_paranoid and carrierwave. The soft delete, hard delete and recover functionality of paranoid work fine. However, when I soft delete an item, the stored image is deleted by carrierwave.
How to configure carrierwave or acts_as_paranoid so that the image is deleted only on hard deletes ?
In the model, add the following line after mount_uploader:
mount_uploader :image, ImageUploader
skip_callback :commit, :after, :remove_image!
Note: it won't affect the other commit callbacks. Eg: after_commit :do_smth would still work.
You can call #model_instance.remove_image! if you want to get rid of the image when doing a hard delete
Related
Does anybody know how to upload a document to later show in a Rails application (as text)? Is Paperclip the right gem to do this? If it is how? (I have uploaded images before with Paperclip).
I like Paperclip. It seems well documented, and has worked well for everything I have needed. (I don't personally know any of them, but the clever folks at Thoughbot have created some pretty useful stuff, for which I feel indebted to them).
Obviously, you need to add Paperclip to your Gemfile, and (if you are using bundler) do your bundle install
Add to your model
has_attached_file :aFile
Add to you controller something to catch whatever you name it in your view (probably in your create and update methods)
#profile.aFile = params[:profile][:aFile]
Probably should check for its existence, if it is a required param
if params[:profile][:aFile].blank?
redirect_to #profile
else
render :action => 'do_something_interesting_with_file'
end
And that's about it. Don't forget your config entries. For example, if you are using some kind of post-processing on the file
Paperclip.options[:command_path] = "/opt/local/bin/"
I found this to be extraordinarily helpful
RailsCast by Ryan Bates
I have created a contact form which will email me once sent. Now I need to be able to have the option of attaching a document that will be emailed to me also. i dont need to save it in a db as it will be with the email and downloaded from there.
Im new to rails so would like to see what other people have done in this situation, ive done some reading and see that i will need the gem paperclip in most instances? Also i have looked at jquery/paperclip in github but this seems a lot of work for what i hope is a small piece of work? I could be wrong here mind, so apologies if i am
Any help greatly appreciated
What you would have to do is use carrierwave gem or paperclip gem to upload the file to a specified folder in your public directory. I would do this using ajax that will allow you to upload the file instantly once you select the file to be sent. This would allow you to upload the attachment and have the file path known and ready so when you click submit, the following code will execute allowing you to send the file. You can also add a line of code to delete the file after it sent successfully if you don't want to accumulate files on your server. More documentation can be found here..
http://guides.rubyonrails.org/action_mailer_basics.html#sending-emails-with-attachments
class ApplicationMailer < ActionMailer::Base
def welcome(recipient)
attachments['free_book.pdf'] = File.read('path/to/file.pdf')
mail(:to => recipient, :subject => "New account information")
end
end
I found a tutorial here https://github.com/plataformatec/devise/wiki/How-To:-Migrate-from-restful_authentication-to-Devise-, however it seems like it is missing pieces of what to do, for example the restful_authentication plugin is still there...how do I remove it? Then how does Devise know what to get for my App...I've tried it but it just keeps breaking my App.
Erase everything in the user.rb controller (or your controller)
Make Sure all of Restful_Authenication is gone (there is quite a bit of includes that may be presnet)
Make sure devise_for :users is present in the routes.rb file
Look for method errors and replace them back in the User.rb controller.
Think that fixed it.
I'm creating a multi-part form in the style that Ryan Bates describes here:
http://railscasts.com/episodes/217-multistep-forms
http://asciicasts.com/episodes/217-multistep-forms (text-based version)
To summarize, I have one view (with a bunch of partials for each form step), and the form variables are stored in a session when the user clicks a next button and a different part of the form is displayed.
One of my form steps allows the user to upload several images via the Paperclip gem. The problem with that is that Rails is trying to upload the image data to the session, which is returning TypeError "can't dump File".
What is a good way to go about this?
UPDATE:
I've tried a bunch of gems (wizardly, acts_as_wizard, and some other smaller ones) but none of them seem to work with Rails 3.
I've also tried just storing the data in an array until the form is complete, but that was causing my controller to get huge and messy.
Saving models into the session is working unless you want to save a File into the session. The wizard plugins are using the session to store models between the steps. They do not produce errors on valid models in my case only on invalids.
So clearing out the attached file sounded a good idea, but in my case clearing out the paperclip attachment with Attachment#clear was not enough because it still wanted to save some File.
I've found out that the problem was with the #queued_for_write attribute in Attachment which still contained the data.
So the following two lines solved my problem:
unless #model.valid?
#model.image.clear
#model.image.queued_for_write.clear
end
This was a paperclip bug and was corrected in this commit.
I wish to use the paperclip gem to add an image that I am retrieving from an API for my user model using code.
How can I attach an image manually using code, rather than passing in parameters from a submitted form ?
If your model object is #bar, image field is called :logo and you want to attach foo.jpg...
#bar[:logo] = File.open('foo.jpg') should work. Or am I misunderstanding the question?
You'd need to initialize your paperclip attachments in a similar way if you were populating from seed data, for example.