

If the user replies "y" or "yes" to this question, then the template will add Devise to the Gemfile outside of any group and then runs the devise:install generator. Then we pose a question to the user about whether or not they would like to install Devise. In the above template we specify that the application relies on the rspec-rails and cucumber-rails gem so these two will be added to the test group in the Gemfile.

blank? generate "devise", model_name end Copy Gem "rspec-rails", group: "test" gem "cucumber-rails", group: "test" if yes? ( "Would you like to install Devise?" ) gem "devise" generate "devise:install" model_name = ask ( "What would you like the user model to be called? " ) model_name = "user" if model_name.

For detailed documentation see the Rails Application Templates guide. This is a brief overview of the Templates API. Now that you've seen how generators can be used inside an application, did you know they can also be used to generate applications too? This kind of generator is referred to as a "template". $ bin/rails generate scaffold Comment body:textĬreate db/migrate/20130924143118_create_comments.rbĬreate app/controllers/comments_controller.rbĬreate test/controllers/comments_controller_test.rbĬreate app/views/comments/Ĭreate app/views/comments/įallbacks allow your generators to have a single responsibility, increasing code reuse and reducing the amount of duplication. We can achieve that by changing our configuration to the following:

The next customization on the workflow will be to stop generating stylesheet and test fixture files for scaffolds altogether. Since each generator has a single responsibility, they are easy to reuse, avoiding code duplication. For instance, the scaffold generator invokes the scaffold_controller generator, which invokes erb, test_unit and helper generators. This allows us to add/replace/remove any of those invocations. The scaffold generator doesn't actually generate anything, it just invokes others to do the work. Looking at this output, it's easy to understand how generators work in Rails 3.0 and above. $ bin/rails generate scaffold User name:stringĬreate db/migrate/20130924151154_create_users.rbĬreate app/controllers/users_controller.rbĬreate test/controllers/users_controller_test.rbĬreate app/views/users/Ĭreate app/views/users/Ĭreate test/application_system_test_case.rb
