简体   繁体   English

删除后,Rails重定向到索引页面

[英]Rails redirect to index page after delete

I need to place delete link at the show page of the object. 我需要在对象的显示页面上放置删除链接。 This show page is to rendered at two different controllers. 该显示页面将在两个不同的控制器上呈现。 In which I need to redirect to the index page from which the request came from. 我需要在其中重定向到请求来自的索引页面。 The URL would be 该URL将是
localhost:3000/users (index page) which will have link to books (show page) that URL will be local host:3000/books/I'd. 本地主机:3000 /用户(索引页面),该页面将具有指向图书(显示页面)的链接,该URL将是本地主机:3000 / books /我想要的。 And at another controller index page local host:3000/books and that too have link to books show page which have delete link in it. 在另一个控制器索引页面上,本地主机:3000 / books,并且也具有指向book show页面的链接,该页面中具有delete链接。 I need to redirect to the index page from which the request came from. 我需要重定向到请求来自的索引页面。 Any help 任何帮助

Edit: How to redirect to previous page in Ruby On Rails? 编辑:如何在Ruby On Rails中重定向到上一页? (Question already there) We can get the request URL at edit action. (已经存在问题了)我们可以在编辑操作中获得请求URL。 But here the delete action will be at common show page 但是这里的删除动作将在普通显示页面上

You could track the visit to the index pages by storing it in the session: 您可以通过将其存储在会话中来跟踪对索引页面的访问:

class ApplicationController
  private 
  def store_location
    session[:stored_location] = request.path
  end

  def stored_location
    session[:stored_location]
  end
end

class UsersController
  before_acton :store_location, only: [:index]
  # ...
end

class BooksController
  before_acton :store_location, only: [:index]
  # ...
end

You can this just use it your destroy action: 您可以使用它来执行销毁操作:

class BooksController
  def destroy
    @book.destroy
    redirect_to stored_location || books_path
  end
end

If I understood your question correctly, you can put the following in your delete controller: 如果我正确理解了您的问题,则可以将以下内容放入delete控制器中:

redirect_to :back

that should redirect back to the page the user came from. 应该重定向回用户来自的页面。

ruby 2.5.1p57 | 红宝石2.5.1p57 | Rails 5.2.0 Rails 5.2.0

Put at the end of your method: 放在方法末尾:

redirect_to action: :index

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

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