'RuntimeError: route set not finalized' wh

2019-08-06 03:38发布

问题:

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