Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • this is a brand new set up, I have just cloned a project and installed ruby, rails, gems, etc
  • I have tried "rvm implode" and starting over many times
  • the rest of the team typically develops locally on Macs, while I am developing on a Ubuntu machine remotely.
  • I'm working as root (does this matter?)

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'

like image 404
Ian Jennings Avatar asked Oct 18 '12 17:10

Ian Jennings


1 Answers

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>
like image 121
Swift Avatar answered Oct 22 '22 17:10

Swift