How to find the local port a rails instance is running on?

前端 未结 6 569
滥情空心
滥情空心 2020-11-30 12:23

So I would like my Rails app instances to register themselves on a \"I\'m up\" kind of thing I\'m playing with, and I\'d like it to be able to mention what local port it\'s

6条回答
  •  无人及你
    2020-11-30 13:01

    Two ways.

    If you're responding to a request in a controller or view, use the request object:

    request.port
    

    If you're in an initialiser and don't have access to the request object use the server options hash:

    Rails::Server.new.options[:Port]
    

提交回复
热议问题