Routes, gems and generators
I have written a gem with an install generator. I would like to use this generator to add routes to the config/routes.rb
file, much in the same way as the devise gem does by adding devise_for :model_name
. Therefore, I need to know how to:
Make a method (开发者_如何学Pythonlike devise_for
) available within the scope of routes?
Ok I've figured it out. To add to the routes file you can use the method route
in the generator. I have accomplished this by adding the following to my install_generator.rb file:
def setup_routes
route("add_gem_routes")
end
Note that I am in fact calling a method, which can be added to the scope of routes by defining it in the following namespace:
module ActionDispatch::Routing
class Mapper
def add_gem_routes
#routing code...
end
end
end
精彩评论