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
eg: http://www.yoursite.com/goldberg/auth/login

<%= request.env['HTTP_HOST']%>
- display base url
eg: www.yoursite.com

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

<%= request.url%>
- full url
eg: http://www.yoursite.com/goldberg/auth/login

<%= request.query_parameters.size %>
- display how many parameters found in current url
url eg: www.yoursite.com/index.html?key=3&d=4
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!! =)

7 comments:

  1. Is there any way to get full url include hash location?
    Eg: www.yoursite.com/users/edit_profile#editprofile-image

    ReplyDelete
  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: www.yoursite.com/users/edit_profile/editprofile-image

    Reference for Mapping: http://guides.rubyonrails.org/action_controller_overview.html

    Example 2)

    Make use of current_page? method
    Refer site: http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-current_page-3F

    Other Examples:
    http://stackoverflow.com/questions/3552228/ruby-on-rails-get-url-string-parameters
    http://stackoverflow.com/questions/152585/identify-get-and-post-parameters-in-ruby-on-rails
    http://stackoverflow.com/questions/4151686/how-to-get-url-parameters-from-a-url-string

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

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

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

    ReplyDelete
  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):]

    ReplyDelete
    Replies
    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.

      Delete