Rails 3 模板:inject_into_file 的奇怪行为

mde*_*tis 2 ruby templates ruby-on-rails

我正在为 Rails 3 编写一个模板(您可以在此处找到它),但我在使用以下几行时遇到了问题:

# 8. config/database.yml modifications
if yes?("Do you want to set the database host to '/var/run/postgresql'?")
  inject_into_file "config/database.yml", " host: /var/run/postgresql\n", :after => "development:\n"


  # FIXME these two won't work :(((( why????
  # inject_into_file "config/database.yml", " host: /var/run/postgresql\n", :after => "production:\n"
  # inject_into_file "config/database.yml", " host: /var/run/postgresql\n", :after => "test:\n"


  # Not finding other solutions, I rewrite the two blocks above with two seds
  run %q(sed -i '/test:/a\ \ host: /var/run/postgresql' config/database.yml)
  run %q(sed -i '/production:/a\ \ host: /var/run/postgresql' config/database.yml)
end
Run Code Online (Sandbox Code Playgroud)

也就是说,第一个 insert_to_file 有效,在 config/database.yml 中我有

development:
  host: /var/run/postgresql
Run Code Online (Sandbox Code Playgroud)

但另外两个inject_to_file已成功应用,但不要修改config/database.yml!所以我有

test:
  adapter: postgresql
Run Code Online (Sandbox Code Playgroud)

production:
  adapter: postgresql
Run Code Online (Sandbox Code Playgroud)

Ste*_*ley 5

您必须指定:force => true多次注入相同文本的选项。看:

http://rdoc.info/github/wycats/thor/master/Thor/Actions#insert_into_file-instance_method

这个版本对我有用:

https://gist.github.com/2573325

不过,不要问我为什么这个选项有意义。