You are here: Forums Ask a Rails expert link_to_remote to empty a fiel...
Posted in Forums : Ask a Rails expert
Authority 0
Posting Rating 69
Sign in to rate this post
|
Hi, I’m using a link_to_remote function to delete a file from a page, but is it possible to empty the field “photo_file_name” (in the db) when I hit the link? Ifso, how can I do this? Here is my code http://pastie.org/529396 |
|
Authority 37
Posting Rating 0
Sign in to rate this post
|
Wouter, @image.update_attribute 'photo_file_name', nil should do the trick…
|
Authority 87
Posting Rating 84
Sign in to rate this post
|
Why not use the PagesController’s #update action? There’s not really a need for an additional action here: https://gist.github.com/6f12ea9c7faad2c9860d |
Authority 37
Posting Rating 94
Sign in to rate this post
|
wouter, you need to update the column “photo_file_name” in the page table with nil. in your controller
|
Authority 0
Posting Rating 69
Sign in to rate this post
|
Thanks for the help, the update_attribute did the trick. |
Authority 50
Posting Rating 99
Sign in to rate this post
|
Wouter, a few things: 1) You should not directly delete files from your file system when you want to destroy an attachment. Paperclip will do this for you if you use it correctly, and this separates your code from the underlying storage implementation. What if you want to use S3 for storage in the future? The code you have here will fail. Let Paperclip do the work for you. 2) You don’t want to use #update_attribute here. The #update_attribute method sets the value of a column in the database without running validations (although it does trigger callbacks). Doing this circumvents the correct behavior of Paperclip, and runs the risk of generating invalid data in your database. Paperclip has a method for removing an attachment: simply set the attachment association to nil. When you save your model Paperclip will eliminate the attachment and delete the associated file for you. For example, in your case you have an attachment association named ‘photo,’ so you would do this: def deleteImage @image = Page.find(params[:id]) @image.photo = nil @image.save! # render some JS end 3) As Pat pointed out, you can do this with your existing pages#update action. I’m guessing when you say you want to do this ‘on the fly’ you mean you’re using an Ajax request. That’s fine, you can reply to both HTML and JS requests using the same action. Assuming, as in Pat’s example, your model has a #clear_photo= method, or something similar to handle removing the attachment, you would simply do this:
def update
@image = Page.find(params[:id])
if @image.update_attributes(params[:page]
respond_to do |format|
format.html { #redirect as normal }
format.js { #render your success JS }
end
else
respond_to do |format|
format.html { render :action => :edit }
format.js { head 500 # or similar }
end
end
end
As an alternative, I prefer to keep image attachment actions in their own controller (Page::ImagesController perhaps). Uploading an attachment would use the #create action, removing the attachment would use the #delete action. Either way will work, and either way is completely RESTful. |
Authority 0
Posting Rating 69
Sign in to rate this post
|
Thanks for your responses, I finally got it working. Sorry for my late response, I had some last exams to finish |
Ask a Rails expert : nested application ApplicationController get called intead of children::ApplicationController
Ask a Rails expert : Best way to structure a database for a large/static dataset
Ask a Rails expert : Ruby Developer (ROR) - Scottish based (Remote working from within the UK)
Ask a Rails expert : Above Ground Pool Supplies
Ask a Rails expert : How to get url params in observer or model in Rails 3.1
Ask a Rails expert : What can persuade you to hire Junior Ruby devs with significant PHP experience?
Ask a Rails expert : What industry value does the Ruby or Rails Certification currently have?
Ask a Rails expert : Louis Vuitton Damier Azur Canvas specially sale ( www.salecheaplouisvuitton.com )
Ask a Rails expert : ·How to check errors/puts statements from ruby files which are under cronob
Ask a Rails expert : Louis Vuitton cheap Soft Sided Luggagespecial offer( www.salecheaplouisvuitton.com )