Getting Started with an existing Rails app
Also consult the instructions for installing on a fresh Rails app, see the React on Rails Basic Tutorial.
If you have Rails 5 API only project, first convert the Rails 5 API only app to a normal Rails app.
-
Add the following to your Gemfile and
bundle install. We recommend fixing the version of React on Rails, as you will need to keep the exact version in sync with the version in yourpackage.jsonfile.gem "shakapacker", "7.0.1" # Use the latest and the exact version gem "react_on_rails", "13.3.1" # Use the latest and the exact versionOr use
bundle add:bundle add shakapacker --version=7.0.1 --strict bundle add react_on_rails --version=13.3.1 --strict -
Run the following 2 commands to install Shakapacker with React. Note, if you are using an older version of Rails than 5.1, you'll need to install Webpacker with React per the instructions here.
rails shakapacker:install -
Commit this to git (or else you cannot run the generator unless you pass the option
--ignore-warnings). -
Run the generator with a simple "Hello World" example (more options below):
rails generate react_on_rails:installFor more information about this generator use
--helpoption:rails generate react_on_rails:install --help -
Ensure that you have
overmindorforemaninstalled.Note:
foremanshould be installed on the system not on your project. Read more -
Start your Rails server:
./bin/devNote:
foremandefaults to PORT 5000 unless you set the value of PORT in your environment. For example, you canexport PORT=3000to use the Rails default port of 3000. For the hello_world example, this is already set. -
Visit localhost:3000/hello_world.
Installation
NPM
All JavaScript in React On Rails is loaded from npm: react-on-rails. To manually install this (you did not use the generator), assuming you have a standard configuration, run this command (assuming you are in the directory where you have your node_modules):
yarn add react-on-rails --exactThat will install the latest version and update your package.json. NOTE: the --exact flag will ensure that you do not have a "~" or "^" for your react-on-rails version in your package.json.