Setting Up Routes In Ruby On Rails
Ruby On Rails (“Rails”) is a powerful language. Five of the most common routes in Rails are Index, Show, Create, Update, and Destroy. Setting up routes in Rails allows you to send data between the front end of your application and the back end of your application. The front end is what the client sees and interacts with, while the back end is the code and database where information is stored.
First, you would need to set up the routes in the routes.rb file. This will allow the end user to access the information contained in your database. By using resources, you can bypass setting up individual routes for the five most common routes. However, if you wanted to create a custom route (such as: post ‘/login’, to: ‘users#create’), then you are unable to use resources.
Once you have set up resources, you then put the controller name directly after resources. In the below example, I used spices as the controller and used the code “only” which will only allow the delineated routes to be utilized. In this example I used the five routes ‘:index, :show, :create, :update, :destroy’.
Rails.application.routes.draw do
resources :spices, only: [:index, :show, :create, :update, :destroy]
end
If you did not use resources, you would have to set up each route individually as shown below.
get ‘/spices’, to ‘spices#index’
get ‘/spices/:id’, to ‘spices#show’
post ‘/spices’, to ‘spices#create’
patch ‘/spices/:id’, to ‘spices#update’
delete ‘/spices/:id’, to ‘spices#destroy’
Next, you will need to set up the spices_controller.rb file.
Index
With the index route you grab all the spices in the Spice array and send them back to the front end. Here, I took a variable and set it equal to Spice.all which will put all the spices into that variable which is then sent to the front end.
def index
spices = Spice.all
render json: spices
end
Show
The show route allows you to get one specific spice. Here, I call on the Spice array and use the find_by() method. By using this method and the id: key I am able to search for a specific id that is passed from the front end. Then I can utilize params[:id] to obtain the id that was sent from the front end.
Below, I set Spice.find_by(id: params[:id]) to a variable and used the render method to send the variable method to the front end in json.
def show
spice = Spice.find_by(id: params[:id])
render json: spice
end
Create
The create route allows you to create a new spice. Here, information is being sent from the front end with the attributes needed to create the new spice.
By calling the create() method on the Spice array you are able to make a new spice in that array.
Then you pass through the keys containing the information about the new spice. These keys include :title, :image, :description, :notes, :rating, and then set Spice.create(:title, :image, :description, :notes, :rating) to a variable.
In the below example, I used the render method to send the newly created spice back to our front end. In addition, I also sent ‘status: :created’ which tells the front end that the new spice was created successfully.
def create
spice = Spice.create(:title, :image, :description, :notes, :rating)
render json: spice, status: :created
end
Update
The update route allows us to update a spice using a similar process as the show and create methods. Here, I take the Spice array and call the find_by() method on the array. I pass through the id: key and use the params[:id] to get the id in the params.
Next, I set Spice.find_by(id: params[:id]) to a variable called spice and use the update() method on that variable and pass through the keys that need to be updated, which are :title, :image, :description, :notes, :rating. This will update the database. Finally, the render method is utilized to send the updated spice to the front end.
def update
spice = Spice.find_by(id: params[:id])
spice.update(:title, :image, :description, :notes, :rating)
render json: spice
end
Destroy
The destroy route allows you to delete/destroy a specific spice. To do this, I used the find_by method on the Spice array and passed through the key id: so that the specific spice that has the same id in the params[:id].
Spice.find_by(id: params[:id]) is set equal to a variable called spice. Then the variable is called on the destroy method which deletes that specific spice from the database.
Finally, I used ‘head :no_content’ which will send back an HTTP response 200 to the front end indicating that the spice was successfully deleted.
def destroy
spice = Spice.find_by(id: params[:id])
spice.destroy
head :no_content
end
I hope this helps when creating your next API. Happy coding! 🤠