【问题标题】:Remove a parameter from URL从 URL 中删除参数
【发布时间】:2016-01-04 21:42:55
【问题描述】:

我的 Rails 应用中有一个 link_to 方法:

link_to t('edit'), edit_building_path(@building, :hidden_action => params[:action])

如何从 url 中删除 hidden_​​action?

当前网址如下:

http://localhost:3000/buildings/2/edit?hidden_action=new

我的问题是我需要知道用户从哪个页面访问这个链接。

【问题讨论】:

  • 这个问题我不清楚。如果您不想在 URL 中添加 hidden_action 参数,请不要将其作为参数附加到方法调用中。
  • 我需要一个 hidden_​​action 参数以便下次在控制器中使用
  • 然后通过会话传递。

标签: ruby-on-rails url parameter-passing link-to


【解决方案1】:

在rails helper中,这取决于你

def edit_the_building_url(building, you_want_the_params_or_not)
  if you_want_the_params_or_not
    edit_building_path(building, :hidden_action => params[:action])
  else
    edit_building_path(building)
  end
end

然后在你的控制器中

link_to t('edit'), edit_the_building_url(@building, true)

link_to t('edit'), edit_the_building_url(@building, false)

如果以上内容对您没有好处,那就

url = url.chomp("?hidden_action=#{params[:action]}")

【讨论】:

    猜你喜欢
    • 2014-10-25
    • 1970-01-01
    • 2018-12-08
    • 2018-07-08
    • 2016-10-09
    • 1970-01-01
    相关资源
    最近更新 更多