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

后端 未结 30 2105
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-22 13:47

How can I get the current absolute URL in my Ruby on Rails view?

The request.request_uri only returns the relative URL

相关标签:
30条回答
  • 2020-11-22 13:58

    You can use:

    request.full_path
    

    or

    request.url
    

    Hopefully it will resolve your problem.

    Cheers

    0 讨论(0)
  • 2020-11-22 14:00

    DEPRECATION WARNING: Using #request_uri is deprecated. Use fullpath instead.

    0 讨论(0)
  • 2020-11-22 14:00

    In Ruby on Rails 3.1.0.rc4:

     request.fullpath
    
    0 讨论(0)
  • 2020-11-22 14:01

    You can either use

    request.original_url 
    

    or

    "#{request.protocol}#{request.host_with_port}" 
    

    to get the current URL.

    0 讨论(0)
  • 2020-11-22 14:02
     url_for(params)
    

    And you can easily add some new parameter:

    url_for(params.merge(:tag => "lol"))
    
    0 讨论(0)
  • 2020-11-22 14:02

    I think request.domain would work, but what if you're in a sub directory like blah.blah.com? Something like this could work:

    <%= request.env["HTTP_HOST"] + page = "/" + request.path_parameters['controller'] + "/" + request.path_parameters['action'] %>
    

    Change the parameters based on your path structure.

    Hope that helps!

    0 讨论(0)
提交回复
热议问题