我刚刚将 Sinatra 应用程序部署到 heroku,该应用程序包含两个 rake 任务:
task :create_db , [:db_id , :db_name]
task :destroy_db , [:db_id, :token]
当我运行时
heroku run rake -T
在控制台中,Heroku 打印以下响应:
(in /app)
rake create_db[db_id,db_name] # Creation count database task
rake destroy_db[db_id,token] # Destroy database task
但是当我运行时:
heroku run rake create_db['test', 'test database']
它返回以下错误:
(in /app)
rake aborted!
Don't know how to build task 'create_db[test,'
/usr/local/lib/ruby/1.9.1/rake.rb:1720:in `[]'
/usr/local/lib/ruby/1.9.1/rake.rb:2040:in `invoke_task'
/usr/local/lib/ruby/1.9.1/rake.rb:2019:in `block (2 levels) in top_level'
/usr/local/lib/ruby/1.9.1/rake.rb:2019:in `each'
/usr/local/lib/ruby/1.9.1/rake.rb:2019:in `block in top_level'
/usr/local/lib/ruby/1.9.1/rake.rb:2058:in `standard_exception_handling'
/usr/local/lib/ruby/1.9.1/rake.rb:2013:in `top_level'
/usr/local/lib/ruby/1.9.1/rake.rb:1992:in `run'
/usr/local/bin/rake:31:in `<main>'
我不太清楚为什么会失败,但我的猜测是它与字符串参数有关。正如您在上面看到的,它说“不知道如何构建任务‘create_db[test,’测试显示没有字符串。
知道如何从命令行运行此任务吗?
最佳答案
来自Rake docs :
The rake task name and its arguments need to be a single command line argument to rake. This generally means no spaces. If spaces are needed, then the entire rake + argument string should be quoted. Something like this:
rake "name[billy bob, smith]"
(Quoting rules vary between operating systems and shells, so make sure you consult the proper docs for your OS/shell).
另请注意,您不需要引用各个参数。
在您的情况下,这应该有效:
rake "create_db[test, test database]"
这也有效:
rake create_db[test,'test database']
请注意,第二个参数被引号括起来,但逗号和引号之间没有空格,因此 shell 将整个参数视为一个参数。
在 Heroku 上
这在使用 heroku run
时不起作用,因为在这种情况下命令被 shell 解释两次,一次由你自己解释,一次在 Heroku Dyno 上,因此您需要确保 Heroku 运行的命令具有正确的引号。
您需要转义引号:
heroku run rake \"create_db[test, test database]\"
因此在 Dyno 上运行的命令是 rake "create_db[test, test database]"
;或引用引号:
heroku run rake "create_db[test,'test database']"
因此运行的命令是 rake create_db[test,'test database']
。
关于ruby - 带有字符串参数的 Sinatra 应用程序的 Heroku rake 任务失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14131662/