{"id":11927,"date":"2025-01-31T04:52:48","date_gmt":"2025-01-31T04:52:48","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=11927"},"modified":"2025-01-31T04:52:48","modified_gmt":"2025-01-31T04:52:48","slug":"get-the-host-and-port-in-rails-applications","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/ruby-on-rails\/get-the-host-and-port-in-rails-applications","title":{"rendered":"How to Get the Host and Port in a Rails App"},"content":{"rendered":"<p>There are various ways to access the host and port in a Rails application. Here are three common approaches:<\/p>\n<ol>\n<li>Passing host and port from the controller to the model<\/li>\n<li>Using environment variables<\/li>\n<li>Using config.action_mailer.default_url_options<\/li>\n<\/ol>\n<h3>1. Pass from Controller to Model:<\/h3>\n<p>In this approach, the host and port are retrieved from the request object in the controller and passed to the model.<\/p>\n<p><strong>Controller:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"ruby\">\r\ndef show\r\n     host = request.host\r\n     port = request.port\r\n  @some_model = MyModel.new_with_url(host, port)\r\nend<\/pre>\n<p><strong>Model:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"ruby\">\r\nclass MyModel < ApplicationRecord\r\n      def self.new_with_url(host, port)\r\n      \tbase_url = \"http:\/\/#{host}:#{port}\/\"\r\n     \t# Use base_url in model logic\r\n   end\r\nend\r\n<\/pre>\n<h3>2. Use Environment Variables:<\/h3>\n<p>Host and port can be stored as environment variables and accessed directly in the model or any other application part.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"ruby\">\r\nhost = ENV[\u2018HOST\u2019]\r\nPort = ENV[\u2018PORT\u2019]\r\n<\/pre>\n<p>This keeps the code decoupled from the request lifecycle and ensures flexibility across different environments.<\/p>\n<h3>3. Use config.action_mailer.default_url_options:<\/h3>\n<p>Host and port can also be configured in the respective environment files, such as <\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"ruby\">\r\nconfig\/environments\/production.rb:\r\nconfig.action_mailer.default_url_options = { host: 'my.application.com', port: 80 }\r\n<\/pre>\n<p>These can then be accessed as follows:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"ruby\">\r\nhost = Rails.application.config.action_mailer.default_url_options[:host]\r\nport = Rails.application.config.action_mailer.default_url_options[:port]\r\n<\/pre>\n<p>This method is particularly useful for generating consistent URLs for system-generated links like emails.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are various ways to access the host and port in a Rails application. Here are three common approaches: Passing host and port from the controller to the model Using environment variables Using config.action_mailer.default_url_options 1. Pass from Controller to Model: In this approach, the host and port are retrieved from the request object in the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":11928,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[11],"tags":[],"class_list":["post-11927","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ruby-on-rails"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/11927"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=11927"}],"version-history":[{"count":1,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/11927\/revisions"}],"predecessor-version":[{"id":11929,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/11927\/revisions\/11929"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/11928"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=11927"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=11927"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=11927"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}