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

后端 未结 30 2109
佛祖请我去吃肉
佛祖请我去吃肉 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 14:14

    if you want to be specific, meaning, you know the path you need:

    link_to current_path(@resource, :only_path => false), current_path(@resource)
    
    0 讨论(0)
  • 2020-11-22 14:15

    You could use url_for(only_path: false)

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

    This works for Ruby on Rails 3.0 and should be supported by most versions of Ruby on Rails:

    request.env['REQUEST_URI']
    
    0 讨论(0)
  • 2020-11-22 14:16

    I needed the application URL but with the subdirectory. I used:

    root_url(:only_path => false)
    
    0 讨论(0)
  • 2020-11-22 14:16

    You can set a variable to URI.parse(current_url), I don't see this proposal here yet and it works for me.

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

    you can use any one for rails 3.2:

    request.original_url
    or
    request.env["HTTP_REFERER"]
    or
    request.env['REQUEST_URI']
    

    I think it will work every where

    "#{request.protocol}#{request.host}:#{request.port}#{request.fullpath}"
    
    0 讨论(0)
提交回复
热议问题