Codementor Events

Your first CRUD application

Published Nov 07, 2020Last updated Nov 08, 2020
Your first CRUD application

The first CRUD application you will create is going to allow you to store in database, update and delete keyboard shortcuts.

crud_action_low.gif

Before you start

You have to have Ruby on Rails and other dependencies installed on your computer.

Go ahead and read this section of my other blog post.

Creating the application

Use this command to create a new Ruby on Rails application:

rails new my_first_rails_app --skip-turbolinks

Turbolinks is a feature something that we do not need to create this application, but you can read more about it here.

Starting the Rails server

Once this command finishes, all you have to do is to run rails server to start the development server and visit http://localhost:3000 on your web browser.

Generating the Shortcut scaffold

Run this command:

rails generate scaffold Shortcut keys description

The output of the command should be similar to this:
generator_output.png

The files that are important for you and should be inspected are:

db/migrate/20201107130259_create_shortcuts.rb
app/models/shortcut.rb
config/routes.rb
app/controllers/shortcuts_controller.rb
app/views/shortcuts/index.html.erb
app/views/shortcuts/edit.html.erb
app/views/shortcuts/show.html.erb
app/views/shortcuts/new.html.erb
app/views/shortcuts/_form.html.erb

Running database migration

Run the command:

rails db:migrate

This will run the database migrations, which includes the migration that was
generated by the rails generate command.

If you see something like this
migrate.png
the table has been created.

Read more about databases migrations here.

Inspecting the CRUD you created

If you now visit the

http://localhost:3000/shortcuts

page you will see the index page of a shortcut resource.

index_view.png

Start by clicking on New shortcut and creating a new record.

Then try to edit it and delete it by returning to list of shortcuts and clicking on "Delete".

With this single command you have made a whole CRUD application where you can:

  • see a list of shortcuts
  • add a new shortcut
  • see shortcut details
  • edit a shortcut
  • delete a shortcut

Conclusion

At the beginning of the post I showed where to find the instructions on how to setup your computer for Ruby on Rails application development.

Then you used the rails new command to create the application as well as started the application development server with rails server.

After that you run the rails generate scaffold command to create the code for the CRUD feature for the shortcut records.

You had to migrate the database to create the shortcuts table so that you could read and write to it.

The rails generate scaffold command is very powerful as it allows you to create a working CRUD feature for a resource in a breeze.

Please, check out the Ruby on Rails guides, to get a deeper understanding on topics discussed in this post.

Contact me here if you are looking for help on Ruby on Rails projects!

Discover and read more posts from Kristians Kuhta
get started