Browse the Ruby on Rails Community.

You are here: Forums Ask a Rails expert link_to_remote to empty a fiel...

Replytotopic

link_to_remote to empty a field

Posted in Forums : Ask a Rails expert

 
Profile

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

 
Bf5f8b9b9b198f2bc52b16d09e48e74e?s=120

Authority 37
Posting Rating 0
Sign in to rate this post

Wouter,

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

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

 
Mahesh_new_image

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

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
 
Profile

Authority 0
Posting Rating 69
Sign in to rate this post

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.)

 
Img_7624

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.

 
Profile

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

Replytotopic

Other Recent Topics

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 )

Formatting Help
  • *bold*       _italics_      
    bq. (quotes)
  • "DSC":http://www.dsc.net
  • * or # (lists)
or cancel