[英]Is there a way to redirect to a specific page after a RESTful delete in Rails?
可以说我有一个消息资源。 在html的某处我有:
<%= link_to("Delete", message, :title => 'Delete', :confirm => 'Are you sure?',
:method => :delete )%>
在我删除它之后,它会将我重定向到列出所有消息的页面。 有没有办法重定向到删除后指定的页面?
在控制器中:
def delete
Item.find(params[:id]).destroy
redirect_to :action => "index"
end
要重定向到最后一个网址,请使用:
redirect_to :back
http://api.rubyonrails.org/classes/ActionController/Base.html#M000662
如果你能学习如何很好地阅读api文档,那么一旦掌握了它们,它们就非常有用。
如果您使用Rails的资源,它实际上是您应该处理的destroy
操作。
def destroy
Item.find(params[:id]).destroy
redirect_to other_specified_path
end
如果你看一下API文档,你会发现ActiveRecord::Base#delete
和ActiveRecord::Base#destroy
方法之间存在巨大差异。 如果你真的明白为什么要使用删除,请使用删除。
我只需要这个
def delete
Item.destroy
redirect_to :back
end
我使用这个,如果我想回到当前页面
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.