【问题标题】:Different Redirect_To in a Destroy action within a Controller Depending on Origin控制器内的 Destroy 操作中的不同 Redirect_To 取决于 Origin
【发布时间】:2012-10-07 23:34:19
【问题描述】:

我目前在控制器中有一个运行良好的销毁功能,但问题是我需要在删除后根据单击该按钮的位置将用户重定向到不同的 url。

例如,如果 user 访问从 index 中看到 他的 记录列表,然后单击删除,他将被重定向回 index (我不能使用 :back 操作,因为它会通过 redirect_to index_path 导致 ActiveRecord::RecordNotFound),如果用户从“查看所有记录”页面单击删除按钮,我需要将用户重定向到 see_all_records_path。

我是否需要创建不同的控制器、动作等?让我知道。非常感谢!

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    您可以在控制器中检查request.referer 并根据该值进行重定向,当单击删除按钮时,它将具有它们所在页面的完整 URL。您可以使用正则表达式或其他方式来确定您应该将该用户重定向到哪里。

    if request.referer.include?('see_all_records')
      ..redirect to see_all_records
    else
      ..redirect to index
    end
    

    【讨论】:

    • 首先感谢您的回复! def destroy @records = Records.find(params[:id]) if @records.present? @records.destroy if request.referer.contains?('profile') redirect_to user_path elsif request.referer.contains?('records') redirect_to see_all_records_path end else redirect_to error_page_path end end 给我错误 undefined method `contains?'
    • 对不起,包括?是您应该使用的,答案已更新。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-16
    • 1970-01-01
    • 1970-01-01
    • 2011-08-22
    • 1970-01-01
    相关资源
    最近更新 更多