问题
When I'm starting a rails app I usually go through the same process of getting my dev env set up. Adding pry-byebug
or various guard-*
gems, and initializing a Guardfile
.
Is there a better way to automate this process?
Some ideas I thought of:
- Creating rake tasks that will add entries to
Gemfile
, runbundle install
, andbundle exec guard init livereload
, etc... - Create a separate command line app that generates the right files and runs the right shell commands.
Is there another simpler way?
回答1:
You can use the templates method described here. You create a file called template.rb
that contains your desired gems, and commands. Then you create a new rails app with
rails new blog -m ~/template.rb
A sample template.rb
is
generate(:scaffold, "person name:string")
route "root to: 'people#index'"
rails_command("db:migrate")
after_bundle do
git :init
git add: "."
git commit: %Q{ -m 'Initial commit' }
end
Another way which I find much easier is just to maintain a github repository which contains everything you want in a bare bones Rails app. Then just git pull
that into a folder when you want to create a new app. The only thing you'd have to overwrite would be the app's name in application.rb
Here is github repo that is a combination of both of sort, it's geared towards programming on Mac but easily changed.
回答2:
See Iceman's answer for more info.
Using rails application templates is a great option.
For example if you want to get up and running with guard-livereload
and pry-byebug
and quickly scaffold a Post
resource, then the following application template (it's just a ruby file) saved in ~/sandboxy.rb
would work:
gem_group :development do
gem 'pry-byebug'
gem 'guard-livereload'
end
run 'bundle exec guard init livereload'
after_bundle do
git :init
git add: "."
git commit: %Q{ -m 'Initial commit' }
end
generate(:scaffold, "post title body:text")
route "root to: 'posts#index'"
rails_command("db:migrate")
git add: '.'
git commit: %Q{ -m 'Scaffold a post' }
Then you can generate a new app with the following command:
rails new some_app -m ~/sandbox.rb
来源:https://stackoverflow.com/questions/42194276/setting-up-rails-dev-env