Building a Full CRUD Rails App

Darrick Pang
3 min readAug 4, 2020

--

CRUD is shorted for create, read, update, delete. When a Rails app uses all four, it is a full CRUD app. Here, I will show you how to build one. First off, we want to create a new user like so.

Fig. 1: Create a new user.

Here, we are creating a new user with a username, email, and password that is replaced by the “user_params” in Create.

Next step is show the user.

FIg. 2: Showing user page.

The “def show” is here to show the user after it was created. From “create”, after the user is created, we redirect it to the user’s page. Another way to show is to use “index” that shows all of the users.

Fig. 3: Showing all users.

Now the app can just create and read. Now let’s show how to update the user.

Fig. 4: Update user.

To update, we need to use “edit” to find the user. Then we want to update the username. The line “@user.update(username: params[:user][:username]) will change the username for the user. Click “update” and it will redirect to the user’s page.

The final thing to do is now destroy. Destroy is meant to delete all info regarding the user, like deleting a social media profile.

Fig. 5: Deleting user.

First, we find the user. Once it is found, then this will delete the user and redirect back to the new user path to create a new user by default.

Now, we can see that creating a full CRUD Rails app is not too difficult. The full program is in figure 6.

Fig. 6: The entire user controller in one.

Lastly, we must discuss routes. The purpose of routes is to determine where we can and cannot go. If we are to build a full Rails CRUD app, we need the routes to be able to go anywhere we want. The default will be just resources :users . Sometimes, we can be explicit and say resources :users, only: [:index, :show, :create, :edit, :update, :destroy] . However, that is not always the case. If we just want to show all, the route would be just resources :users, only: [:index] . Another for just all users and click the name to see one is resources :users, only: [:index, :show] . If we want edit, we add edit and update to it.

So this shows a basic way to build a full CRUD app for Rails. Writing the files here is a great way to start.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response