Member-only story
A How-to Guide to Set Up A Database with Generators
Building a backend can feel stressful and you may feel like there’s a copious amount of information to remember, especially if you’re just starting out. Once you get the basics down, you can kick on the generators and relax.
Let’s start from the ground up. We just created a new Ruby on Rails application with rails new <project name>
and now we want to start building out our project.
From this point, we can generate all sort of things; assets, controller, generator, helper, inherited_resources_controller, integration_test, mailer, migration, model, observer, performance_test, resource, responders_controller, scaffold, scaffold_controller, session_migration, and task. We’ll address controller, model, migration, and resource in this post.
All of the generators follow the same outline rails generate <name of generator> <options>
. Adding --no-test-framework
to the end of this command, won’t generate a spec file with tests.
Note: g
can be used in place of generate
Controller
rails generate controller users
will:
create app/controllers/users_controller.rb
invoke erb
create app/views/users
invoke test_unit
create test/controllers/users_controller_test.rb
invoke helper…