简体   繁体   English

在Rails中进行RESTful删除后,有没有办法重定向到特定页面?

[英]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: 在html的某处我有:

<%= 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 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. 如果你能学习如何很好地阅读api文档,那么一旦掌握了它们,它们就非常有用。

It's actually the destroy action that you should be dealing with if you're using Rails' resources. 如果您使用Rails的资源,它实际上是您应该处理的destroy操作。

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. 如果你看一下API文档,你会发现ActiveRecord::Base#deleteActiveRecord::Base#destroy方法之间存在巨大差异。 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 我使用这个,如果我想回到当前页面

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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