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

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

    You can add this current_url method in the ApplicationController to return the current URL and allow merging in other parameters

    # https://x.com/y/1?page=1 
    # + current_url( :page => 3 )
    # = https://x.com/y/1?page=3
    def current_url(overwrite={})
        url_for :only_path => false, :params => params.merge(overwrite)
    end
    

    Example Usage:

    current_url --> http://...
    current_url(:page=>4) --> http://...&page=4
    

提交回复
热议问题