What’s the Best Way to Integrate React into Rails? A Step-by-Step Guide

/

Overview:-

  • Discover the best ways to integrate React into Rails with clear, step-by-step guidance.
  • Compare sprinkling React components vs full API mode for modern apps.
  • Learn setup commands, CORS fixes, deployment strategies, and debugging tricks.

You love the way Rails handles the backend, routes, models, and data; it’s ready to go, so you use that. But the front end? ERB templates and jQuery start to feel clunky pretty quickly. Users want slick UIs, instant updates and React is just the thing for this.

The question is this: how do you best integrate React and Rails together for your app? You have two main ways: sprinkle some React into Rails views, or build a separate React frontend, with Rails as your API.

This guide takes you through both choices, why they matter and keeps it practical. No fluff, no theory, just a simple guide for Rails and React integration.

Step-by-step to integrate React into Rails

The first thing you need to do is to choose how you want to integrate. This is the first step in the guide. Ask yourself:

  • Do you just need interactive sprinkles on mostly server-rendered views?
  • Or do you want a full-blown React app that treats Rails like an API?

Here’s the short version:

  • Option 1: Sprinkle React into Rails views.
    Great if your app is mostly server-rendered, but you need modern UI in a few spots.
  • Option 2: Full API mode with Rails backend + React frontend.
    Perfect for building SPAs where React is the primary UI layer.

Pick an approach based on your goal. Now, let’s see how we can integrate for both options

Option 1: “Sprinkling” React with the react-rails Gem

This step is for option 1 – when you just want a dynamic widget here and there

Step 1: Install the gem in your Gemfile:

gem 'react-rails'

Step 2: Then run:

bundle install
rails webpacker:install
rails webpacker:install:react
rails generate react:install

Step 3: Make your first component:

rails generate react:component HelloWorld greeting:string

Step 4: Throw it in your view (ERB):

<%= react_component("HelloWorld", { greeting: "Hello from Rails & React!" }) %>

React now works right inside your Rails template.

Why is this good?

  • Fast setup, zero hassle.
  • You can pass Rails data straight into React props.
  • Minimal disruption if your app’s structure is already Rails-centric.

Start here if you’re experimenting. It’s a no-risk way to test React without rebuilding your app.

Option 2: Go Full API With a Separate React Frontend

If you’re ready to build a “real” single-page app, keep these codebases separate. Rails does the backend; React (maybe with Create React App or Vite) does the UI. They talk over JSON APIs.

Step 1:  Set up your back-end

In one folder:

rails new myapp --api

Step 2: Set up your front-end

In another folder:

npx create-react-app frontend

Step 3:

  • Now connect the two. Have React make fetch or axios calls to /api/something

Step 4:

CORS warning!
Install the rack-cors gem and add something like this to config/initializers/cors.rb:

Rails.application.config.middleware.insert_before 0, Rack::Cors do
  allow do
    origins '*'
    resource '*', headers: :any, methods: [:get, :post, :patch, :put, :delete, :options]
  end
end

Warning: For production, never use ‘*’ for origins. Lock it down!

Why go this route?

  • Clean separation of backend and frontend.
  • More flexibility—deploy React wherever (Netlify, Vercel).
  • Easier scaling as your app grows

Managing Assets and Hot Reloading

A smooth development workflow is just as important as the integration itself. Webpacker is installed by default when you use the react-rails gem. 

You get ES6 support, module imports, and hot reloading without additional setup. This keeps Rails and React working in harmony within a single codebase.

When you maintain separate Rails and React applications, you’ll need to run both servers during development:

rails s

npm start

To streamline this setup:

  • Configure your React development server to proxy requests to the Rails API.
  • Use environment variables to manage URLs and API keys instead of hardcoding.
  • In production, serve your React build separately (Netlify, Vercel, or similar) to reduce deployment complexity.

The goal here is efficiency: fast reloads, clear separation of responsibilities, and minimal friction as you build and test features.

Deploy and Maintain

You’ll hit bumps. Everyone does. Here are the ones that bite most devs:

  • Typos in component names: React won’t render. Check casing.
  • CORS misconfigurations: If fetch calls fail, recheck your origins in rack-cors.
  • Routing mismatches: Rails wants to own routes. React Router wants to own them too. Set up your React index to receive unknown routes from Rails.
  • Webpacker version:  If bundling fails, ensure your gem and node versions are aligned.

80% of debugging boils down to silly mistakes. Avoid overanalyzing and compare the console and Rails logs.

Quick Reference: Which Path Should You Take?

This quick reference can help you make a quick decision:

  • You want: quick interactivity without major rewrites → Use react-rails gem.
  • You need: scalable SPA with React Router, Redux, etc. → Go API-only Rails + React frontend.
  • You care about: simple deployment → Gem path is easier.

You value: frontend freedom → Option 2 win.

Pro Tips for Deployment and Maintenance

Here are some pro tips that you can keep in mind to take your development to the next level.

  • Run tests before adding React: Make sure your Rails base is stable.
  • APIs Version: Without proper versioning, your React frontend will outpace your Rails backend.
  • Optimize for production: Precompile assets if using gem. Or let Netlify handle build for separate React app.
  • Logs are your friend: Keep browser console and Rails logs open when debugging.
  • Use environment-specific configs: Don’t hardcode URLs or keys.

Conclusion

Finally, it’s not about code snippets, it’s about what you need. If your app just needs a small portion of React, don’t overcomplicate things. Drop in react-rails and move on. 

If you’re building a rich, modern product that demands React’s full power, then separate the concerns, Rails as API, React as frontend, and let each do what it does best.

The point isn’t choosing the fanciest setup. It’s giving your users the smoothest experience without making things hard for yourself. 

Rails already handles the backend. React takes care of the UI. The real “best way” is the one that gets you deploying faster and debugging less.

Overview:-

  • Discover the best ways to integrate React into Rails with clear, step-by-step guidance.
  • Compare sprinkling React components vs full API mode for modern apps.
  • Learn setup commands, CORS fixes, deployment strategies, and debugging tricks.

You love the way Rails handles the backend, routes, models, and data; it’s ready to go, so you use that. But the front end? ERB templates and jQuery start to feel clunky pretty quickly. Users want slick UIs, instant updates and React is just the thing for this.

The question is this: how do you best integrate React and Rails together for your app? You have two main ways: sprinkle some React into Rails views, or build a separate React frontend, with Rails as your API.

This guide takes you through both choices, why they matter and keeps it practical. No fluff, no theory, just a simple guide for Rails and React integration.

Step-by-step to integrate React into Rails

The first thing you need to do is to choose how you want to integrate. This is the first step in the guide. Ask yourself:

  • Do you just need interactive sprinkles on mostly server-rendered views?
  • Or do you want a full-blown React app that treats Rails like an API?

Here’s the short version:

  • Option 1: Sprinkle React into Rails views.
    Great if your app is mostly server-rendered, but you need modern UI in a few spots.
  • Option 2: Full API mode with Rails backend + React frontend.
    Perfect for building SPAs where React is the primary UI layer.

Pick an approach based on your goal. Now, let’s see how we can integrate for both options

Option 1: “Sprinkling” React with the react-rails Gem

This step is for option 1 – when you just want a dynamic widget here and there

Step 1: Install the gem in your Gemfile:

gem 'react-rails'

Step 2: Then run:

bundle install
rails webpacker:install
rails webpacker:install:react
rails generate react:install

Step 3: Make your first component:

rails generate react:component HelloWorld greeting:string

Step 4: Throw it in your view (ERB):

<%= react_component("HelloWorld", { greeting: "Hello from Rails & React!" }) %>

React now works right inside your Rails template.

Why is this good?

  • Fast setup, zero hassle.
  • You can pass Rails data straight into React props.
  • Minimal disruption if your app’s structure is already Rails-centric.

Start here if you’re experimenting. It’s a no-risk way to test React without rebuilding your app.

Option 2: Go Full API With a Separate React Frontend

If you’re ready to build a “real” single-page app, keep these codebases separate. Rails does the backend; React (maybe with Create React App or Vite) does the UI. They talk over JSON APIs.

Step 1:  Set up your back-end

In one folder:

rails new myapp --api

Step 2: Set up your front-end

In another folder:

npx create-react-app frontend

Step 3:

  • Now connect the two. Have React make fetch or axios calls to /api/something

Step 4:

CORS warning!
Install the rack-cors gem and add something like this to config/initializers/cors.rb:

Rails.application.config.middleware.insert_before 0, Rack::Cors do
  allow do
    origins '*'
    resource '*', headers: :any, methods: [:get, :post, :patch, :put, :delete, :options]
  end
end

Warning: For production, never use ‘*’ for origins. Lock it down!

Why go this route?

  • Clean separation of backend and frontend.
  • More flexibility—deploy React wherever (Netlify, Vercel).
  • Easier scaling as your app grows

Managing Assets and Hot Reloading

A smooth development workflow is just as important as the integration itself. Webpacker is installed by default when you use the react-rails gem. 

You get ES6 support, module imports, and hot reloading without additional setup. This keeps Rails and React working in harmony within a single codebase.

When you maintain separate Rails and React applications, you’ll need to run both servers during development:

rails s

npm start

To streamline this setup:

  • Configure your React development server to proxy requests to the Rails API.
  • Use environment variables to manage URLs and API keys instead of hardcoding.
  • In production, serve your React build separately (Netlify, Vercel, or similar) to reduce deployment complexity.

The goal here is efficiency: fast reloads, clear separation of responsibilities, and minimal friction as you build and test features.

Deploy and Maintain

You’ll hit bumps. Everyone does. Here are the ones that bite most devs:

  • Typos in component names: React won’t render. Check casing.
  • CORS misconfigurations: If fetch calls fail, recheck your origins in rack-cors.
  • Routing mismatches: Rails wants to own routes. React Router wants to own them too. Set up your React index to receive unknown routes from Rails.
  • Webpacker version:  If bundling fails, ensure your gem and node versions are aligned.

80% of debugging boils down to silly mistakes. Avoid overanalyzing and compare the console and Rails logs.

Quick Reference: Which Path Should You Take?

This quick reference can help you make a quick decision:

  • You want: quick interactivity without major rewrites → Use react-rails gem.
  • You need: scalable SPA with React Router, Redux, etc. → Go API-only Rails + React frontend.
  • You care about: simple deployment → Gem path is easier.

You value: frontend freedom → Option 2 win.

Pro Tips for Deployment and Maintenance

Here are some pro tips that you can keep in mind to take your development to the next level.

  • Run tests before adding React: Make sure your Rails base is stable.
  • APIs Version: Without proper versioning, your React frontend will outpace your Rails backend.
  • Optimize for production: Precompile assets if using gem. Or let Netlify handle build for separate React app.
  • Logs are your friend: Keep browser console and Rails logs open when debugging.
  • Use environment-specific configs: Don’t hardcode URLs or keys.

Conclusion

Finally, it’s not about code snippets, it’s about what you need. If your app just needs a small portion of React, don’t overcomplicate things. Drop in react-rails and move on. 

If you’re building a rich, modern product that demands React’s full power, then separate the concerns, Rails as API, React as frontend, and let each do what it does best.

The point isn’t choosing the fanciest setup. It’s giving your users the smoothest experience without making things hard for yourself. 

Rails already handles the backend. React takes care of the UI. The real “best way” is the one that gets you deploying faster and debugging less.

logo

Soft Suave - Live Chat online

close

Are you sure you want to end the session?

💬 Hi there! Need help?
chat 1