We’ve briefly touched on CRUD in a previous post showcasing the powerful Rails generators, but what exactly is CRUD and how is it used? CRUD stands for Create, Read, Update, and Delete. One and/or more of these are the basic functions of most apps, the ability for the user to create, read, update, and delete content. You know from my previous blog that if you generate a new Rails app using the scaffold generator, the basic crud functions will already be written in, but this also adds a lot of extra stuff to your projects you probably wont need, so how do we add CRUD functionality to our Rails apps ourselves? First have your app created with the basic models and controllers, I recommend using the resource generator for this. Now, go to your routes.rb file in the config folder and make sure it routes as a resource, this opens routes for all CRUD functions:
Rails.application.routes.draw do
resources :users
end
If you don’t want to allow the User to access all CRUD functions, you can change your routing to only inlcude the ones you want, for instance, this routing does not allow users to edit their profiles:
Rails.application.routes.draw do
get 'users/index'
get 'users/show'
get 'users/new'
#get 'users/edit' <- commented out, will not use
end
Next, create the resource generator should have created your database tables, check the migration tables to be sure in your db folder and migrate the data using this command in the terminal:
rails db:migrate
Now, go into your controller (in this case, the user controller) and here we will fill out all of our CRUD actions, first the Read, creating an index that allows us to view all users:
def index
@users = User.all
end
you can now use @users in your view app to iterate through and display all the users!
To view a specific User (also Read), you can create a show function:
def show
@user = User.find(params[:id])
This will find the user by their table ID in the database.
To create new users, you can make a function like so:
def create
@user = User.new(user_params)
redirect_to user_path(@user)
end
This creates a new user, then redirects them to their user page/dashboard. In most applications though, you will want to verify that the new user is valid by checking things like uniqueness of name or password length. To do that, in your user model, add some validators like so:
class User < ApplicationRecord
has_secure_password
has_many :posts
has_many :comments
has_many :communities, through: :posts
validates :name, presence: true
validates :name, uniqueness: true
validates :email, presence: true
validates :email, uniqueness: true
validates :password, presence: true
validates :password, length: { minimum: 6 }
end
and your create function in the controller will change to allow for a validation, if it passes then nothing changes, if it fails it will show an error and ask the user to try again:
def create
@user = User.new(user_params)
if @user.valid?
@user.save
session[:user_id] = @user.id
redirect_to user_path(@user)
else
render :new
end
end
Now, you have a new user created, but wait, you want to change the username, an email address, or your bio… To do that we use the Update method of CRUD. Add this to your controller:
def update
@user = User.find(params[:id])
@user.update(user_params)
redirect_to user_path(@user)
end
This will successfully update your user! For the last CRUD function, delete/destroy, you can use the following method, this will update the database to delete the users data from it:
def destroy
@user = User.find(params[:id])
@user.destroy
end
And thats it! You are now able to implement basic CRUD functions into your Rails app!