简体   繁体   中英

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

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM