在一个相对新的 Rails 项目中,
Rakefile
的内容如下:# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
require File.expand_path('../config/application', __FILE__)
Blog::Application.load_tasks
然而,rake routes
会输出以下内容:
cpe-74-72-73-47:rails-blog-example djechlin$ rake routes
home_index GET /home/index(.:format) home#index
root / home#index
我不明白rake
是如何工作的,以便它可以访问路由文件或路由任务。根据命令行使用文档,rake
是这样调用的:
rake [options ...] [VAR=VALUE ...] [targets ...]
但是这个页面没有解释目标是什么。我猜测rake
直接在routes.rb
文件中调用,而与此无关的Rakefile
,但我无法确认这一点。
rake routes
首先运行Rakefile
,该文件调用AppName::Application::load_tasks
,创建一个名为:routes
的任务,然后由于它作为参数传递给了rake
,所以该任务被执行。对吗? - djechlin