【问题标题】:Rails 4 - What is the difference between request.original_fullpath and request.fullpathRails 4 - request.original_fullpath 和 request.fullpath 有什么区别
【发布时间】:2016-03-06 04:14:30
【问题描述】:

我需要将用户搜索查询存储在我们的数据库中以跟踪搜索历史。我知道 request.original_url 会将查询字符串作为绝对 url 给我。

http://www.example.com/search?utf8=%E2%9C%93&keywords=cars&view=grid

我更喜欢存储相对 url 路径。话虽如此,对于所有参数的相对 url,request.original_fullpathrequest.fullpath 有什么区别?他们似乎是同一件事?

request.original_fullpath

/search?utf8=%E2%9C%93&keywords=cars&view=grid

request.fullpath

/search?utf8=%E2%9C%93&keywords=cars&view=grid

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 uri query-string


    【解决方案1】:

    original_fullpath 返回一个字符串,其中包含最后请求的路径,包括它们的参数。

    fullpath 返回字符串完整路径,包括最后请求的 URL 的参数。

    original_fullpathfullpath 的区别在于,original_fullpath 方法不包含原始 url 中没有的参数(即通过 POST 而不是 GET 发送的参数)。

    【讨论】:

    • 感谢您的回复。它对我来说仍然有点困惑。就我而言,听起来我想使用完整路径,因为我们的搜索使用的是 GET 请求方法,有时一些请求是通过 ajax 完成的。我想确保始终捕获完整的请求 url。
    • @Bryan,确切地说,original_fullpath 不会包含 POST 方法参数,所以如果你总是想捕获参数,使用 fullpath 是要走的路。如果您有任何其他问题,请告诉我。
    【解决方案2】:

    我想添加 original_fullpath 忽略重定向,而 fullpath 包括它们,例如:

    # some_spec.rb
    describe 'collections' do
      before do
        get '/collections'
      end
      context 'user is not signed in' do
        it 'should redirect to /unauthenticated' do
          expect(request.original_fullpath).to eq '/collections'
          expect(request.fullpath).to eq '/unauthenticated'
        end
      end
    end
    

    【讨论】:

      猜你喜欢
      • 2015-08-15
      • 2020-01-01
      • 2022-11-22
      • 2022-11-27
      • 2014-11-30
      • 2023-03-26
      • 1970-01-01
      • 2017-06-06
      • 2021-12-13
      相关资源
      最近更新 更多