首页 > 解决方案 > 带有 url 查询参数选项的 Rails 5 redirect_back

问题描述

根据文档

redirect_back(fallback_location:,allow_other_host:true,**args)

  • :fallback_location- 将在缺少Referer标题时使用的默认后备位置。

  • :allow_other_host- 允许或禁止重定向到与当前主机不同的主机,默认为 true。

  • 可以传递给 redirect_to 的所有其他选项都被接受为选项,并且行为是相同的。

    redirect_to允许我通过将参数作为散列传递来将参数添加到 url

    那么,为什么这些都不适合我:

  • redirect_back fallback_location: tasks_path, allow_other_host: false, syncing: true
  • redirect_back fallback_location: tasks_path, allow_other_host: false, { syncing: true }
  • redirect_back fallback_location: tasks_path, allow_other_host: false, options: { syncing: true }
  • redirect_back(fallback_location: tasks_path, allow_other_host: false, options: { syncing: true })
  • redirect_back(fallback_location: tasks_path, allow_other_host: false, syncing: true)
  • ...以及我能想到的上述任何其他迭代。

    所有这些(都是有效代码),只需在不添加参数的情况下返回我

    我正在尝试实现此 URL: (back_url or fallback_location) + '?syncing=true'

    标签: ruby-on-rails-5

    解决方案


    如果您查看源代码redirect_back您会看到,它本质上使用redirect_to "whatever_url.com"了方法的版本redirect_to

    如果您检查redirect_to 的解释,您会发现在这个用例中您很遗憾无法传递任何参数。如果这是非常需要的,我想你可以重写redirect_back将 params 选项附加到带有字符串连接的 url 的方法,但这似乎是一个令人讨厌的修复。

    但是要回答您的问题-您想要实现的目标似乎是不可能的。


    推荐阅读