Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you get the app name when running Rails on Heroku?

This means app-name.heroku.com.

Note this is different from Rails.application.class.parent_name.

Rails.application.class.parent_name is defined in the application.

Working in Rails 3.

like image 335
B Seven Avatar asked Sep 20 '11 16:09

B Seven


People also ask

How do I find my Heroku app name?

By default, a Heroku app is available at its Heroku domain, which has the form [name of app]. herokuapp.com . For example, an app named serene-example-4269 is hosted at serene-example-4269.herokuapp.com .

Are Heroku app names unique?

After a second or two, Heroku will have created a new app and have chosen a random name for it. This ensures that your app name is globally unique across the entire Heroku platform, making it possible to use the app name as a part of your domain name.


3 Answers

The solution with ENV['URL'] will only work during requests.

So if you need to know the app id outside a request, you's set a config variable like this

heroku config:add APP_NAME=<myappname> --app <myappname>

And enable lab feature that allows you to use them during compile

heroku labs:enable user-env-compile -a myapp

And now I have my app name available here:

ENV["APP_NAME"] # '<myappname>'

This is handy if you want to load different config file (say with oauth credentials) based on the app's name or id.

like image 123
hakunin Avatar answered Oct 05 '22 07:10

hakunin


Heroku actually sets a URL variable in the environment by default

app_name = ENV['URL'].split(".").first

Referenced here: http://devcenter.heroku.com/articles/config-vars and http://ididitmyway.heroku.com/past/2010/11/9/sinatra_settings_and_configuration/

update: actually the URL variable might not be there by default, but you could always add an environment variable "app name" a priori, unless you were trying to avoid that approach all together.

update 2: indeed, the other, obvious but limiting approach, would be to grab the domain off the request variable, but this limits you to your controller. http://guides.rubyonrails.org/action_controller_overview.html#the-request-object

like image 44
CambridgeMike Avatar answered Oct 05 '22 06:10

CambridgeMike


Rails.application.config.session_options[:key].sub(/^_/,'').sub(/_session/,'')
=> "test-app"

that's the name of the Rails app, as it was spelled when you did 'rails new app-name'

Using this, you could do this:

class << Rails.application
  def name
    Rails.application.config.session_options[:key].sub(/^_/,'').sub(/_session/,'')
  end
end

Rails.application.name
=> 'test-app'
like image 23
Tilo Avatar answered Oct 05 '22 07:10

Tilo