A Guide to Creating A CRUD App with Ruby On Rails

What is Ruby On Rails?

What is CRUD?

Or, if you want to view all of your public stories, you can follow the logical path through the URL link: View my profile (/me), my stories (/stories), and then all of my public stories(/public).

Creating the CRUD App

2. CD into your application:

3. Generate models for your application. The ‘g’ is short for ‘generate’ and the two can be used interchangeably. We’ll be creating a Pokemon model that has a table with two columns: name and level. After generating the model, two files will be created: a pokemon.rb file in your models folder and [date_time]_create_pokemons.rb file in your migrations folder.

4. Next we’ll be creating a controller for the Pokemon model but, unlike the model, the controller must be plural. (Unfortunately, the plural of Pokemon has to be Pokemons for this example.) This will automatically create a few things for us!

  • “pokemons_controller.rb” inside the Controllers folder
  • “dogs” folder that contains the four html.erb files we specified (index, show, new, edit)

5. The config/routes.rb file will automatically be updated. Since we want a fully functioning app, we’ll specify all of the resources we want to allow.

6. Next we will want to set up our database tables and migrate our database to create a schema file. You’ll want to double check your migration files that were created before and tweak everything before running ‘rails db:migrate’.

7. Next we’re going to test our app to make sure it works. Add some pokemon to the db/migrate/seeds.rb file before running rails db:seed.
Tip: make sure to have a clear_all function before your seed data so that you refresh your database every time you run rails db:seed!

8. Type “rails console” or “rails c” to start up the rails testing console. Type in “Pokemon.all” to make sure that the database seeded correctly.

Adding the Read Function

Go to the pokemon controller in app/controllers/pokemons_controller.rb and create an instance variable to access all the Pokemon inside the index view.

Go to the index view inside the views folder views/pokemons/index.html.erb and use the <% (evaluates but doesn’t print) and <%= (evaluates and prints) operators to write a simple block to output each pokemon’s name and level.

Next we want to add a way to show each specific Pokemon on their own page. Our goal now is to show a specific pokemon at a pokemon_path.

We need to go to the pokemon controller again and create an instance variable to access one specific Pokemon inside the show view. We’ll be finding the pokemon by their database key id number.

After that, we’ll be going to the show view inside the views folder views/pokemons/show.html.erb, using the <% and <%= operators again to output the pokemon’s details.

Adding the Create Function

Again, go to the pokemon controller, but this time we’ll be creating two methods. One to populate the new form with an empty pokemon object shell, and one to save the information gathered into that shell as a new pokemon and then redirect us to view the new pokemon we created.

Go to the new view inside the views folder. We want to create the form that can be filled out. Since pokemon have names and levels we added fields for both to be filled in.

Making the Edit Function

Back to the pokemon controller we go, but this time we’re making an edit and an update method! The edit method is finding the pokemon we would like to edit and the update method is updating that pokemon with the params (the allowed editable things) and then redirecting to that newly updated pokemon’s show page.

Go to the edit view inside the views folder. The form will be auto-populated with the current information to make it easier to edit!

Making the Delete Function

There are so many different ways and places to put a delete button, but for now, we’ll just put it as a button inside a form. Not the best, but it’ll do.

What Now?

Full-Stack Developer, Software Engineer, and UX/UI Aficionado

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store