Ian Jennings
Ian Jennings

Reputation: 2319

Subdomains + ActionView::Template::Error (Missing host to link to!)

I have worked through the numerous solutions to the error described in the title.

ActionView::Template::Error (Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true):

However, this project has also modified the url_for function to make use of subdomains, as seen in this railscast:

http://railscasts.com/episodes/221-subdomains-in-rails-3

So, the traditional answers to this error, such as setting the variables in my environment settings don't seem to be the solution.

Here are some other hints:

Completed 500 Internal Server Error in 1889ms

ActionView::Template::Error (Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true): 1: %header.menu{:role => "banner"} 2: .col980 3: %h1 4: %a.logo{:href => root_url({:subdomain => false})} 5: -if current_user.premium? 6: %img{:alt => "Contently", :src => "/images/logo_beta_premium.png"}/ 7: -else app/helpers/url_helper.rb:16:in url_for' app/views/shared/_logged_in_writer_nav.html.haml:4:in_app_views_shared__logged_in_writer_nav_html_haml__656388632_107925510' app/views/layouts/application.html.haml:35:in block in _app_views_layouts_application_html_haml__193634629_107212530' app/helpers/application_helper.rb:15:inhtml5_haml_tag' app/views/layouts/application.html.haml:2:in _app_views_layouts_application_html_haml__193634629_107212530' app/controllers/application_controller.rb:18:inerror_generic'

Upvotes: 6

Views: 1514

Answers (1)

Swift
Swift

Reputation: 13188

The problem is that you're using a url helper without providing a default host to use for the application. The magic of *_url is that it returns the path along with the base url in the link.

For example, if your default url host is example.com:

> link_to "All Blogs", root_url(:subdomain => false)
#=> <a href="http://example.com/">All Blogs</a>

You can set up the default host in your config/environments/*.rb files by adding the following lines to the bottom of the environment config file you're in.

config.before_initialize do                                                                                                                                                                                                       
  MyApp::Application.routes.default_url_options[:host] = 'myapp.com'
end

Edit:

You can completely avoid this problem by using *_path

> link_to "All Blogs", root_path
#=> <a href="/">All Blogs</a>

Upvotes: 6

Related Questions