Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to redirect to a specific page after a RESTful delete in Rails?

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?


1 Answers

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.

like image 119
CodeJoust Avatar answered Dec 20 '25 16:12

CodeJoust



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!