开发者

Rails: remove port from _url helper

When calling the _url helper in rails it will return host+port+path so for example

photo_url

will return

http://localhost:3000/photo

In 开发者_开发问答my production environment I have thin serving the rails application on a given port and apache serving static content on another port. When the site is accesed without any port each server knows wich part to handle and everything is fine. However if a specific port is given, only the requested server sends a response (which is expected).

I'm now running into troubles, because when I authenticate a user via oauth2 (facebook, twitter) oder openid I need to send an callback url. The gem I use (OmniAuth) uses the _url helper (as far as I can tell) (callback_url) to calculate the callback url. Which results in only thin responding to further requests because of the appended port number.

Is there a way to tell rails, that it is not running on any specific port? Or that it does not use the portnumber in the _url helper?


OmniAuth does not use a helper, but rather uses its own method which parses request.url to calculate the full path unless you provide configuration yourself.

OmniAuth's callback_url:

def callback_url
  full_host + callback_path
end

OmniAuth's full_host :

def full_host
  case OmniAuth.config.full_host
    when String
      OmniAuth.config.full_host
    when Proc
      OmniAuth.config.full_host.call(env)
    else
      uri = URI.parse(request.url)
      uri.path = ''
      uri.query = nil
      uri.to_s
  end
end

So, if you access the page on http://localhost:3000/whatever, OmniAuth will use http://localhost:3000 as the full_url. If the page is accessed on http://mysite.com/whatever, the full_url will be http://mysite.com. Thus, the port number of the thin server serving the page should not be appended to the URL, unless the URL used to access the page that redirects the user to the callback includes the port number.

Note that, if necessary, you can configure OmniAuth to use a fixed full_host by setting OmniAuth.config.full_host to a String or Proc that returns the value you want to use.


Can you use photo_path instead?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜