views:

118

answers:

3

Lets say I have a message resource. Somewhere in the html I have:

<%= link_to("Delete", message, :title => 'Delete', :confirm => 'Are you sure?', 
   :method => :delete )%>

Right after I delete it, it redirects me to the page where it lists all the messages. Is there a way to redirect to a page that I specify after the deletion?

+6  A: 

In the controller:

def delete
  Item.find(params[:id]).destroy
  redirect_to :action => "index"
end

To redirect to the last url, use:

redirect_to :back

http://api.rubyonrails.org/classes/ActionController/Base.html#M000662

If you can learn how to read api docs well, they are extremely useful, once you get the hang of them.

CodeJoust
Specifically, change line 3 to point to where you want it to go.
MattMcKnight
A: 

It's actually the destroy action that you should be dealing with if you're using Rails' resources.

def destroy
  Item.find(params[:id].destroy
  redirect_to other_specified_path
end

If you look at the API documentation, you'll see there's a HUGE difference between the ActiveRecord::Base#delete and ActiveRecord::Base#destroy methods. Only use delete if you really understand why you're using it.

bensie
A: 

i only need this

def delete
  Item.destroy
  redirect_to :back
end

i used this if i want to back to current page

Kuya