ruby on rails - RubyOnRails: url_for application root -
i know doing
url_for(:only_path => false, :controller => 'home')
i get, example, http://localhost/home
but how handle genereate http://localhost
depending on goals are, there few ways use server name or base url. general case of, "i need reliable base url can use anywhere," use config method.
# via routes.rb map.root :controller => "foo", :action => "bar" # view/controller: root_url # inflexible. root_url ever 1 url # via request object url_for("http://"+request.host) # not available in models # via config file (see railscast 85) # environment.rb app_config = yaml.load_file("#{rails_root}/config/config.yml")[rails_env] # config/config.yml development: server_name: localhost:3000 production: server_name: foo.com # view/controller: url_for(app_config('server_name'))
Comments
Post a Comment