Welcome to Working With Rails

 

Discussion Forums

Discuss all things Ruby on Rails with perhaps the web's most vibrant group of Ruby on Rails enthusiasts.
link_to_remote to empty a field
7 Posts
link_to_remote to empty a field

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

Wouter,

@image.update_attribute 'photo_file_name', nil should do the trick...

Why not use the PagesController's #update action? There's not really a need for an additional action here: https://gist.github.com/6f12ea9c7faad2c9860d

wouter,

you need to update the column "photo_file_name" in the page table with nil.

in your controller

def deleteImage

@image = Page.find(params[:id])
if !@image.photo_file_name.nil?
   File.delete("#{RAILS_ROOT}/public/images/frontend/#{@image.photo_file_name}")
   @image.update_attribute( :photo_file_name=> nil)
 end

end

Thanks for the help, the update_attribute did the trick. The reason why I do not use the update action is that I'd like to delete the file on the fly. (so I think its easier to do it with update_attribute.)

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.

Thanks for your responses, I finally got it working.

Sorry for my late response, I had some last exams to finish

7 Posts
Login to add your message