2012-04-25 11 views
1
私はRailsの3用のテンプレートを書いています

の奇妙なbeahaviourは(あなたはそれをhereを見つけることができます)、私は次の行とのトラブルを抱えている:、最初のですRailsの3テンプレート:inject_into_file

# 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 

作品inject_to_file、とのconfig/database.ymlの中で私が

development: 
    host: /var/run/postgresql 

を持っているが、他の2 inject_to_fileが正常に適用されますが、設定/ database.ymlを変更しないでください!だから私はあなたが同じテキストを複数回注入する:force => trueオプションを指定する必要が

production: 
    adapter: postgresql 
+0

は、あなたはそれが( '\ r')トリガからの置換を妨げている、あなたの' database.yml'(例えば実行 'OD -c'変な文字や余分な改行などの「明白な」ものではないのですがチェックありファイルにすべてのものがあるはずであることを確認してください)。 – Casper

答えて

2

私は少し異なる問題への解決策を探して、ここでつまずい:

ドキュメントが私のためにすべての時間を働いていなかったこと、しかし、このように

inject_into_file 'name_of_file.rb', after: "#The code goes below this line. Don't forget the Line break at the end\n" do <<-'RUBY' 
    puts "Hello World" 
RUBY 
end 

をinject_into_file使用することを言うレールが。場合によっては、実際に "puts"という単語とルビコードを私が注入しようとしていたファイルに出力します。

最後に、inject_into_file文字列を渡すことができるとわかりました。それはずっと簡単です。

inject_into_file 'name_of_file.rb', :after => "#The code goes below this line. Don't forget the Line break at the end\n" do 
    "Hello World" 
end 
+1

'inject_into_file'が' append_to_file'に変更されました:http://rdoc.info/gems/thor/0.18.1/Thor/Actions:append_to_file – mdesantis

+0

場所の前後に渡すオプションはありませんしかし、append_to_file? – sarink

+0

私は間違いを犯しました: 'inject_into_file'のエイリアスを持つ' insert_into_file'です:http://rdoc.info/gems/thor/0.18.1/Thor/Actions#insert_into_file-instance_method – mdesantis