我正在为应用程序创建一个自定义模板,我添加了要使用的 pg 而不是 sqlite:
gem 'pg'
gsub_file "Gemfile", /^gem\s+["']sqlite3["'].*$/,''
after_bundle do
generate "rspec:install"
generate "simple_form:install"
end
但是,如果我这样创建应用程序,database.yml 文件如果默认为 sqlite3 配置,我如何指定应用程序添加 postgresql
的配置而不是从我的模板?就像我添加 --database=postgresql
选项一样
最佳答案
不需要向用户询问应用程序名称,因为 app_name
已经填充了它。
Thor命令对于处理文件非常有帮助,可以使用 .erb
模板并使用 template method 将模板中的值传递给它们:
# config the app to use postgres
remove_file 'config/database.yml'
template 'database.erb', 'config/database.yml'
数据库.erb:
default: &default
adapter: postgresql
host: db
port: 5432
pool: 5
timeout: 5000
user: postgres
password: postgres
development:
<<: *default
database: <%= app_name %>_development
test:
<<: *default
database: <%= app_name %>_test
production:
<<: *default
database: <%= app_name %>_production
关于ruby-on-rails - 如何在 Rails 应用程序模板中使用 postgres?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35418577/