Fetch from a Custom Route in Ruby on Rails

Wendy You
CodeX
Published in
3 min readOct 28, 2022

Anyone who has used Ruby on Rails can appreciate just how efficient it can be — it’s a developer’s dream! With one quick line in the terminal, we can create migration, model, controller, and serializer files for a desired class.

$ rails g resource Noodle shape base origin

gives us this terminal output:

Migration, model, controller, and serializer files created when running rails g resource in terminal.

In addition to the above files, this one line also adds “resources” for this class’s corresponding table in the database.

# routes.rbRails.application.routes.draw do
resources :noodles
end

From just this one line in our routes file, we are automatically given our RESTful routes. In other words, each resource line sets up the routes for our five main actions: index, show, create, update, and destroy.

# routes.rbresources :noodles# the one line above does the same job as the five lines below combinedget ‘/noodles’, to: ‘noodles#index’get ‘/noodles/:id’, to: ‘noodles#show’post ‘/noodles’, to: ‘noodles#create’patch ‘/noodles/:id’, to: ‘noodles#update’delete ‘/noodles/:id’, to: ‘noodles#destroy’

All we have to do now is define those CRUD actions in our controller file. Easy peasy, done!

But what if we needed another route to fetch data from, one that does not fall into the five main routes we are given? For example, perhaps each of our noodles has reviews, and we want to grab only the reviews for a specific noodle rather than all the reviews in our database. Enter custom routes!

Setting It Up

Custom routes are set up in the same style as those we get from resources, as seen in the above image. We start with our HTTP verb (get, post, put, patch, delete), then our URL path as a string, a comma, “to:”, and lastly the controller and action name separated by a hashtag and as one string:

# routes.rb# route templateHTTP verb 'URL path', to: 'controller#action'# applied to an example get ‘/noodles/:noodle_id/reviews’, to: ‘reviews#specific_noodle’

Once this line is saved in our routes.rb file, the next step is to head to the controller file we specified and define the action we specified:

# reviews_controller.rbdef specific_noodle
render json: Review.where(noodle_id: params[:noodle_id])
end

Using It

Now that we have our very own unique route, the best part is being able to see the data that we wanted in the first place! We can test that our route works by running our server, then heading to Postman or to our browser where our server is hosted. Remember: only get requests can be tested in the browser — use Postman for all other HTTP requests.

How to test our new custom route in Postman.
How to test our new custom route in the browser.

If we approve of the returned data, we can now apply this route to our frontend fetch requests. The URL that we fetch from will have the same endpoint as the URL path in our custom route. The method in our fetch will also match the method in our custom route.

// Frontend.js (frontend file)fetch(`/noodles/${noodle.id}/reviews`)
.then(res => res.json())
// Recall our custom route URL path: ‘/noodles/:noodle_id/reviews’

And that’s it!

While Ruby on Rails gives us plenty of shortcuts, it’s been designed to be easily customizable, too. Let the backend do the heavy lifting and sift out the exact desired data using a custom route!

References

--

--

Wendy You
CodeX
Writer for

Software Engineer | Background in Chemistry and Marketing | Solutions Seeker | Lifetime Learner