Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 4 - What is the difference between request.original_fullpath and request.fullpath

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
like image 331
Bryan.I Avatar asked Dec 02 '15 02:12

Bryan.I


People also ask

What is request referer in rails?

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?

What is the best way to get the current request URL in Rails? You should use request. original_url to get the current URL.

How do I get the current URL in Ruby on Rails?

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.


2 Answers

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
like image 110
con-- Avatar answered Sep 29 '22 08:09

con--


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).

like image 21
K M Rakibul Islam Avatar answered Sep 29 '22 08:09

K M Rakibul Islam