-
run
rails new rails_react_boilerplate -d=postgresql --webpack=react --skip-coffee
-
add lines to
config/database.yml
below line 19 (under thedefault
database):
username: postgres
password: <%= ENV['RAILS_REACT_BOILERPLATE_DATABASE_PASSWORD'] %>
-
run
export RAILS_REACT_BOILERPLATE_DATABASE_PASSWORD="<your_postgres_password>" && rails db:create
-
run
rails g controller Homepage index && rm app/helpers/homepage_helper.rb app/assets/stylesheets/homepage.scss
and replace line 2 fromconfig/routes.rb
file withroot 'homepage#index'
-
run
mv app/javascript/packs/hello_react.jsx app/javascript/packs/Index.jsx
-
add lines to
app/views/layouts/application.html.erb
after line 9 (inside header):
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<%= javascript_pack_tag 'Index' %>
-
remove all content from
app/views/homepage/index.html.erb
-
run
yarn add react-router-dom
-
create file
app/javascript/components/Example.jsx
with the following content:
import React from 'react'
const Example = props => (
<div>This is an example!</div>
)
export default Example
- create file
app/javascript/routes/Index.jsx
with the following content:
import React from "react";
import { BrowserRouter as Router, Route, Switch } from "react-router-dom";
import Example from "../components/Example";
export default (
<Router>
<Switch>
<Route path="/example" exact component={Example} />
</Switch>
</Router>
);
- create file
app/javascript/components/App.jsx
with the following content:
import React from "react";
import Routes from "../routes/Index";
export default props => <>{Routes}</>;
- replace file
app/javascript/components/App.jsx
content with the following content:
import React from 'react'
import ReactDOM from 'react-dom'
import App from '../components/App'
document.addEventListener('DOMContentLoaded', () => {
ReactDOM.render(
<App/>,
document.body.appendChild(document.createElement('div')),
)
})
- add this line to
config/routes.rb
file:
get '/*path' => 'homepage#index'