简体   繁体   中英

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?

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.

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.

i only need this

def delete
  Item.destroy
  redirect_to :back
end

i used this if i want to back to current page

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM