Wednesday, June 23, 2010

Ruby on Rails: Request Url

<%= request.request_uri %> / <%= request.path%>
- display current url without base url
eg: /goldberg/auth/login

<%= request.referer %>
- display previous url with base url

<%= request.env['HTTP_HOST']%>
- display base url

<%= request.env['SERVER_PROTOCOL']%>
- display protocol
eg: HTTP/1.1

<%= request.url%>
- full url

<%= request.query_parameters.size %>
- display how many parameters found in current url
url eg:
answer: 2 => (2 parameters)

<%= request.path_parameters['controller'] %>
- display controller name from current url
eg: goldberg/auth

<%= request.path_parameters['action'] %>
- display action name from current url
eg: login

<%= request.inspect%>
- It will tell you everything thats inside request.(Long request)

If you find this useful, would you like to buy me a drink? No matter more or less, it will be an encouragement for me to go further. Thanks in advance!! =)


  1. Is there any way to get full url include hash location?

  2. I only can think of these two methods, you may have a try..

    Example 1)

    Create a map connection in config/routes.rb.
    map.connect "/users/edit_profile/:status", :controller => "users", :action => "edit_profile"

    Then, you can get the parameter in views like this, <%= params[:status] %>

    Full url should be: <%= request.url+params[:status] %>
    ## url:

    Reference for Mapping:

    Example 2)

    Make use of current_page? method
    Refer site:

    Other Examples:

  3. I tired your methods.It is not good .so please create another method.

  4. Thank you very much for this!

    It took me a while to find so it's a great relief that someone posted it! I can't believe how rude sathya above was...

  5. dai Anonymous oru elavum puriyila da.... sathya what u said was absolutely perfect

  6. don't know why i use [= "#{request.original_url}"] in my slim view page, it tells me that [ActionView::Template::Error (undefined method `original_url' for nil:NilClass):]

    1. Please check the Rails version that you use, request.original_url is for Rails 3.2 or Rails 4.

      For Rails 3: Use "#{request.protocol}#{request.host_with_port}#{request.fullpath}", as request.url is now deprecated.

      For Rails 2: Use request.url instead of request.request_uri.

  7. It's very nice blog and useful for students and developer ,
    Ruby on Rails Online Training

  8. It's very nice blog and useful for students and developer ,
    Ruby on Rails Online Training