Last active
July 7, 2023 21:17
-
-
Save dmoulton/6b6b8f91a2cd46befca882c967867eab to your computer and use it in GitHub Desktop.
Process to create monolithic rails and react app
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
consult https://blog.appsignal.com/2022/08/03/connect-a-ruby-on-rails-app-with-react-in-a-monolith.html | |
rails new <app name> -d <database> -j esbuild | |
cd into app | |
./bin/rails db:create | |
./bin/rails s | |
generate a home controller -> ./bin/rails g controller Home index | |
update routes to make home#index the root | |
yarn add react react-dom node-uuid | |
update the build portion of packages.json to be: | |
esbuild app/javascript/*.* --bundle --sourcemap --outdir=app/assets/builds --public-path=assets --loader:.js=jsx | |
modify views/application.html.erb to have this body | |
<body> | |
<div id="app"></div> | |
</body> | |
app/javascript/application.js should change to: | |
//-------- | |
import React from "react"; | |
import { createRoot } from "react-dom/client"; | |
import App from "./App"; | |
const container = document.getElementById("app"); | |
const root = createRoot(container); | |
root.render(<App />); | |
//--------- | |
app/assets/config/manifest.js should be changed to this: | |
//----------- | |
//= link_tree ../images | |
//= link_directory ../stylesheets .css | |
//= link application.js | |
//----------- | |
create App.js in app/javascript and add the base component | |
you can start both rails and yarn with | |
./bin/dev | |
You may get an foreman error with the above. If so then uninstall and reinstall the foreman gem | |
If you want to start the 2 servers separately, use | |
yarn build --watch | |
rails s | |
in different terminals |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment