How to Create a REST API with Ruby on Rails
Hey, picture us at the bar, me, Stefano, sharing how I cracked this API thing. How to create rest api with ruby on rails was my recent fix for a side gig, and I'm laying it out just like that. The thing is, handling user data can be a pain if you're lost, but Rails makes it straightforward if you follow along.
But let's get to it: we're building a basic REST API for CRUD on users, like creating, reading, updating, and deleting records. It's no big deal, yet super handy to start with.
First off, make sure you have Ruby, say 3.1 or up, and RailsāI stick with 7 since it's rock solid. Oh, and Git for tracking changes, otherwise you'll mess up your commits. Alright, but if you're missing any, just grab them via terminal; it's a couple of commands.
And now, dive in. Start by making a new Rails project: open your terminal and type rails new user_api --api. This sets up just the API bits, no extra fluff for the front. I prefer this because it skips the bloat that slows down deploys.
Next, generate the user model: rails g model User name:string email:string. That creates the database file. But watch out, you gotta migrate it after. Run rails db:migrate to make it real. The last time I skipped that, I wasted a weekend fixing dumb errors, so don't be like me.
Ok, onto the controller. Use the generator: rails g controller Users. In app/controllers/users_controller.rb, add the CRUD actions. For instance, for the index: def index; render json: User.all; end. It's straightforward, but seriously, render json is a game-saver for API outputs.
For routes, open config/routes.rb and put: resources :users. Done, you get the routes auto-magically. But hey, if you want to tweak, like only GET and POST, go ahead; I keep it full to avoid headaches later.
Time to test. Fire up the server with rails s and hit localhost:3000/users. You should see an empty list. Try creating a user with Postman or curl: send a POST with { "name": "Stefano", "email": "stefano@example.com" }. Works? Awesome, but if not, check the logsāthat's where the gremlins hide.
Steps for Deploying
How to create rest api with ruby on rails isn't complete without deploying it. Go with Heroku; it's easy for newbies. First, add the gem in your Gemfile: gem 'pg' for the database, then bundle install.Create an app on Heroku via CLI: heroku create my-user-api. Push your code with git push heroku main. And for the database, heroku run rails db:migrate. I've tried other platforms, but frankly Heroku sucks for heavy stuff, though it's fine for a tutorial like this.
Common Issues and Tips
But here's a tip: sometimes HTTP permissions block you, like CORS. Add the gem 'rack-cors' and set it up in config/initializers/cors.rb. It's a hassle, but needed if your API talks to a frontend.And to wrap it up practically, head to heroku open and test your live API. If all's good, you've got a deployed API working. Give it a shot yourself, add a user and see if it sticks.
Oh, and if you're reading this, remember coding's like a good beerāyou gotta sip it slow to enjoy it.