子域名 + ActionView::Template::Error(缺少链接的主机!)

6

我已经尝试了许多解决方案来解决标题中描述的错误。

ActionView :: Template :: Error(缺少要链接到的主机!请提供:host参数,设置default_url_options [: host]或将:only_path设置为true):

但是,该项目还修改了url_for函数以利用子域,如在此railscast中所示:

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

因此,传统的解决此错误的方法,例如在环境设置中设置变量似乎不是解决方案。

以下是一些其他提示:

  • 这是一个全新的设置,我刚刚克隆了一个项目并安装了ruby,rails,gems等
  • 我尝试过“ rvm implode”并多次重新开始
  • 团队的其他成员通常在Mac上本地开发,而我在远程Ubuntu机器上进行开发。
  • 我正在以root身份工作(这很重要吗?)

1889ms内完成500个内部服务器错误

ActionView :: Template :: Error(缺少要链接到的主机!请提供:host参数,设置default_url_options [: host]或将:only_path设置为true):     1:%header.menu {:role =>“banner”}     2:.col980     3:%h1     4:%a.logo {:href => root_url({:subdomain => false})}     5:-如果current_user.premium?     6:%img {:alt =>“Contently”,:src =>“/ images / logo_beta_premium.png”} /     7:-否则   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:in html5_haml_tag'   app / views / layouts / application.html.haml:2:in _app_views_layouts_application_html_haml__193634629_107212530'   app / controllers / application_controller.rb:18:in error_generic'

1个回答

6
问题在于您正在使用url助手,但没有提供应用程序使用的默认主机。 *_url 的神奇之处在于它返回链接中的路径以及基本url。例如,如果您的默认url主机是example.com:
> link_to "All Blogs", root_url(:subdomain => false)
#=> <a href="http://example.com/">All Blogs</a>

您可以通过在config/environments/*.rb文件的底部添加以下行来设置默认主机,以此来与您当前的环境配置文件匹配。

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

编辑:

你可以通过使用*_path完全避免这个问题。

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接