假设我有一个消息资源。在html中的某处,我有:
<%= link_to("Delete", message, :title => 'Delete', :confirm => 'Are you sure?',
:method => :delete )%>
在我删除它之后,它会将我重定向到列出所有消息的页面。有没有办法在删除后重定向到我指定的页面?
发布于 2009-12-07 03:55:16
在控制器中:
def delete
Item.find(params[:id]).destroy
redirect_to :action => "index"
end
要重定向到最后一个url,请使用:
redirect_to :back
http://api.rubyonrails.org/classes/ActionController/Base.html#M000662
如果你能学会如何很好地阅读api文档,那么一旦你掌握了它们的诀窍,它们就会非常有用。
发布于 2009-12-07 06:23:04
如果您正在使用Rails的资源,那么它实际上是您应该处理的destroy
操作。
def destroy
Item.find(params[:id]).destroy
redirect_to other_specified_path
end
如果你看API文档,你会发现ActiveRecord::Base#delete
和ActiveRecord::Base#destroy
方法之间有很大的不同。只有当你真正理解为什么要使用delete时,才使用它。
发布于 2009-12-07 13:37:05
我只需要这个
def delete
Item.destroy
redirect_to :back
end
如果我想返回到当前页面,我使用了这个
https://stackoverflow.com/questions/1857711
复制