views:

33

answers:

1

Hi, I want to change my delete not to delete but to update a field on the record called "deleted"

<%= link_to 'Destroy', document, :confirm => 'Are you sure?', :method => :delete %>

Seems like this should work, but it doesnt: <%= link_to 'Destroy', document, :deleted => true, :confirm => 'Are you sure?', :method => :post %>

+1  A: 

There's no automatic way to do this through a view helper such as link_to. Your actions in your controller need to actually do this for you.

If you never want to delete a document through your destroy action, then just rewrite that action to set its 'deleted' attribute to true instead of actually destroying it. That way you can continue to use your original link. so:

class Document < ActiveRecord::Base

  # DELETE /documents/:id
  def destroy
    @document = Document.find(params[:id])
    @document.deleted = true
    @document.save
    redirect_to documents_path
  end
end

with

<%= link_to 'Destroy', document, :confirm => 'Are you sure?', :method => :delete %>
brad
This is the right way to do it, although if you want to simplify that code a bit, I would use: Document.find(params[:id]).update_attribute(:deleted, true)
Brett Bender
I thought about this some more, is there a way to do this with the url, what if I want to undelete? or just update any given attribute. Doing a while form and styling the button is another way I have been doing this, but it seems ugly.
Joelio
not sure i really know what you mean with your "while form... " but if you want to update any given attribute, use the update action :) You seem to be confused by some of the rails magic. You don't want an action just for updating one attribute, you update a whole whack of them through update with params. Undelete, you can create a custom action called undelete and do the opposite of above. Just route your link properly...
brad