'RuntimeError: route set not finalized' when calling Rails.application.routes.generate() or Rails.application.routes.recognize_path()

妖精的绣舞 提交于 2020-01-15 11:43:12

问题


I use the Rails.application.routes.generate() and Rails.application.routes.recognize_path() in my application to disassemble and generate URLs using the Rails routing table. I never had a problem with this using the Rails 2.3.x series but since Rails 3 (specifically 3.1.3) I've been getting the following error when invoking these commands

RuntimeError: route set not finalized

Two questions :

  • Why? Am I getting this - my application is up and running and handling other requests. Why are the routes not finalized by now?
  • To solve this error I call Rails.application.routes.finalize! before invoking either of these methods as it seems to work. Are there any implications to doing this?

回答1:


In Rails 3, you don't use this technic. You need include Rails.application.routes.url_helpers after you can use your named_route

class User < ActiveRecord::Base
  include Rails.application.routes.url_helpers

  def my_own_url
    user_url(self)
  end
end


来源:https://stackoverflow.com/questions/9291366/runtimeerror-route-set-not-finalized-when-calling-rails-application-routes-ge

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!