Skip to content

Commit 0116db5

Browse files
committed
brush up
1 parent 662b0b0 commit 0116db5

File tree

15 files changed

+24
-169
lines changed

15 files changed

+24
-169
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ This is an example web application proposing a way to integrate React Router Fra
55
Consider using this if you wish to easily create a better React SPA integrated with Ruby on Rails.
66

77
[Jump to how to build it](#how-it-is-built)
8+
[See this example app deployed using Kamal](https://rrrails.castle104.com)
89

910
## The problem
1011

@@ -167,10 +168,9 @@ This ensures that assets use the browser cache effectively, whereas the HTML tem
167168

168169
### Deployment
169170

170-
The React Router build step is integrated into the `bin/rails assets:precompile` task.
171+
Yarn installation and the React Router build step are integrated into the `bin/rails assets:precompile` task.
171172
Artifacts are stored inside the Ruby on Rails `public` folder.
172173
Note that we don't use Propshaft for the React Router build, and therefore artifacts are not saved into `app/assets/builds`.
173174

174175
Your Dockerfile and CI setup can stay the same with the exception that you will need to install Node.js.
175-
If you want to learn how to do this, creating a new rails app configured with jsbundling is a great way
176-
to learn how to write a good Dockerfile that installs Node.js.
176+
See the `Dockerfile` for an example.

app/controllers/react_controller.rb

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,19 @@ class ReactController < ApplicationController
44
# This is the catch-all action for React Router. This will render the index.html file
55
# for all React Router requests.
66
#
7-
# Unlike typical webpack or esbuild setups, we do not render ERB files which use
7+
# Unlike typical webpack or esbuild setups, we do not render ERB files which include
88
# `javascript_include_tag` (propshaft, sprockets) or `javascript_pack_tag` (webpack)
99
# to load the React app.
10-
# Instead, we render the index.html file that was generated by the React Router build
11-
# and renamed to "react-router-index.html".
10+
# Instead, take the index.html file that was generated by the React Router build
11+
# and rename it to "react-router-index.html".
12+
# This is served as a file from the controller in response to the bootstrap file request.
1213
#
13-
# This setup allows us to better benefit from the optimizations that React Router includes
14-
# in its build.
14+
# Benefits:
1515
#
16-
# The response will also send cookies and other headers set in the controller.
16+
# * We can use the index.html file that React Router generates as is.
17+
# This file contains optimizations that would be challenging to recreate on the Rails-side.
18+
# * By going through the Rails controller, we can adjust cache and cookie headers to
19+
# improve performance, reliability, and integration with Rails.
1720
def show
1821
render file: Rails.root.join("public", "react-router", "react-router-index.html")
1922
end

app/controllers/sessions_controller.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
# We have implemented a very simply but mostly sufficient login system to
2+
# illustrate how easy it is to add authentication when done on Rails,
3+
# thanks to the robust session framework that it provides.
4+
#
5+
# Obviously lacking is a password hashing mechanism which Rails provides out of the box
6+
# as the `use_secure_password` method on ActiveRecord.
7+
# We use simple clear text passwords here.
18
class SessionsController < ApplicationController
29
# GET /users/new
310
def new
Lines changed: 1 addition & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,9 @@
11
class UsersController < ApplicationController
2-
before_action :set_user, only: %i[ show edit update destroy ]
3-
4-
# GET /users or /users.json
5-
def index
6-
@users = User.all
7-
end
8-
9-
# GET /users/1 or /users/1.json
10-
def show
11-
end
12-
132
def me
143
if @user = current_user
15-
render :show
4+
render :me
165
else
176
head :no_content
187
end
198
end
20-
21-
# GET /users/new
22-
def new
23-
@user = User.new
24-
end
25-
26-
# GET /users/1/edit
27-
def edit
28-
end
29-
30-
# POST /users or /users.json
31-
def create
32-
@user = User.new(user_params)
33-
34-
respond_to do |format|
35-
if @user.save
36-
format.html { redirect_to @user, notice: "User was successfully created." }
37-
format.json { render :show, status: :created, location: @user }
38-
else
39-
format.html { render :new, status: :unprocessable_entity }
40-
format.json { render json: @user.errors, status: :unprocessable_entity }
41-
end
42-
end
43-
end
44-
45-
# PATCH/PUT /users/1 or /users/1.json
46-
def update
47-
respond_to do |format|
48-
if @user.update(user_params)
49-
format.html { redirect_to @user, notice: "User was successfully updated." }
50-
format.json { render :show, status: :ok, location: @user }
51-
else
52-
format.html { render :edit, status: :unprocessable_entity }
53-
format.json { render json: @user.errors, status: :unprocessable_entity }
54-
end
55-
end
56-
end
57-
58-
# DELETE /users/1 or /users/1.json
59-
def destroy
60-
@user.destroy!
61-
62-
respond_to do |format|
63-
format.html { redirect_to users_path, status: :see_other, notice: "User was successfully destroyed." }
64-
format.json { head :no_content }
65-
end
66-
end
67-
68-
private
69-
# Use callbacks to share common setup or constraints between actions.
70-
def set_user
71-
@user = User.find(params.expect(:id))
72-
end
73-
74-
# Only allow a list of trusted parameters through.
75-
def user_params
76-
params.expect(user: [ :email, :clear_password ])
77-
end
789
end

app/views/users/_form.html.erb

Lines changed: 0 additions & 27 deletions
This file was deleted.

app/views/users/_user.html.erb

Lines changed: 0 additions & 12 deletions
This file was deleted.

app/views/users/edit.html.erb

Lines changed: 0 additions & 12 deletions
This file was deleted.

app/views/users/index.html.erb

Lines changed: 0 additions & 16 deletions
This file was deleted.

app/views/users/index.json.jbuilder

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)