开发者

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

开发者 https://www.devze.com 2023-04-06 09:19 出处:网络
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.

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.

Wo开发者_JS百科rking in Rails 3.


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.


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


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'


If you're running in different environments in each app per this, you could just set a variable in staging.rb or production.rb.

0

精彩评论

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