获取RuntimeError:"为了使用#url_for,您必须明确包含路由助手"当我已经包含它们时

时间:2017-01-27 21:15:39

标签: ruby-on-rails ruby ruby-on-rails-4 actionview

在控制器内部,我已经尝试运行此代码,以便当已登录的用户偶然发现注册页面时

def index
  if current_user
    redirect_to homebase_url #should provide url to home for logged in users
  end
end

我已经完成了rails错误消息所说的内容并添加了include Rails.application.routes.url_helpers到包含的控制器类。仍然得到这个错误。绝对不希望将URL硬编码到那里用于遗留目的。感谢

2 个答案:

答案 0 :(得分:3)

删除include Rails.application.routes.url_helpers声明,除非它像帮手一样,否则不需要它。默认情况下,路由包含在控制器中。将它包含在模型或控制器/初始化程序中(路由在初始化程序之前加载)是针对MVC体系结构的,可能会导致不必要的行为。

答案 1 :(得分:1)

我建议使用homebase_path代替homebase_url

例如,在查看http://stackoverflow.com/questions/41903124/getting-runtimeerror-in-order-to-use-url-for-you-must-include-routing-helper/42048113#42048113

console.log(window.location.pathname) // => "/questions/41903124/getting-r..."
console.log(window.location.host) // => "stackoverflow.com"

路径是域名(主机)之后的内容。

完整的网址(在您的情况下为homebase_url)需要host和路径。在开发过程中,获得主机有点尴尬,因为它在生产中的价值与开发中的价值不同。