I need to store user search queries in our db for tracking search history. I know that request.original_url will give me the query string as an absolute url.
http://www.example.com/search?utf8=%E2%9C%93&keywords=cars&view=grid
I would prefer storing the relative url path. With that said, for a relative url with all params what is the difference between request.original_fullpath and request.fullpath? They seem to be the same thing?
request.original_fullpath
/search?utf8=%E2%9C%93&keywords=cars&view=grid
request.fullpath
/search?utf8=%E2%9C%93&keywords=cars&view=grid
request.referer gives you the previous URL or / if none. It is usually used to redirect the user back to the previous page (link)
What is the best way to get the current request URL in Rails? You should use request. original_url to get the current URL.
You can write request. url instead of request. request_uri . This combines the protocol (usually http://) with the host, and request_uri to give you the full address.
I'd like to add original_fullpath ignores redirects, while fullpath includes them, for example:
# 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
original_fullpath returns a String with the last requested path including their params.
fullpath returns the String full path including params of the last URL requested.
The difference between original_fullpath
and fullpath
is that, original_fullpath
method doesn’t include parameters that weren’t in the original url (i.e. parameters that were sent via POST instead of GET).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With