If you like it, give this project some love by giving it a star!
Overall this is a simple todo-app that uses Rails backend and React frontend. Perfect to learn how to communicate between two different projects.
I used this as my reference to get started. This part will cover the API part. To see the frontend/ client part, check out the rerails-client repo.
-
Set up a basic Rails App without views by adding
--api
in the end of new command.rails new AppName --api
(I am using Rails 5.0.0+) -
Create the todos controller and basic CRUD methods.
rails generate controller Todos index show create destroy
(Ok, maybe CRD since I didn't haveUpdate
. I will maybe addUpdate
method later). The methods are pretty simple. Feel free to peek at mytodos_controller.rb
to see what I have. The only thing that is different is that it is now rendering onlyjson
instead ofhtml
. -
Don't forget the route!
resources :todos, only: [:index, :show, :create, :destroy]
. In my case, I usedapi
namescope. Check myroutes.rb
for more info. -
We need to create a simple todo model in database. Let's do so:
rails generate model Todo description:string
, thenrails db:migrate
-
Right now we have a simple Rails api running. You can run
rails server
and go tolocalhost:3000/api/todos
. If you do it right, you should see all your todos that you have made injson
format. You can create them fromrails console
or seed them (copy myseeds.rb
then runrails db:seed
, then restart the server). -
This is a good time to switch to client side and set up components to display todos using
GET
request. Both fetch'sPOST
andDELETE
methods are left as an exercise. Feel free to take a look at the rerails-client repo for hints. -
Once we get the basic React setup and proxied to 3001, we will setup foreman gem. Follow the instruction to install foreman on Rails.
-
Create a
Procfile
to run the following commands:web: cd ../rerails-client && npm run start
andapi: rails server -p 3001
. The former line tells API to go to client-app and run the React app. The directory path may be changed to suit your directory. The latter tells rails to run rails server proxied tolocalhost:3001
, the location where React app was set to proxy to. In short, Rails will run onlocalhost:3001
while React will run onlocalhost:3000
and React will look atlocalhost:3001
to get its data from. -
Finally, run
foreman start -p 3000
. If you go tolocalhost:3000
, you will see the client app. If you go tolocalhost:3001
, you will see the rails app. These two will be running simultaneously.
That's it! Now you have two apps running at the same time. If you have any question, feel free to let me know!