|
| 1 | +# 🤖 AI Agent Instructions: React on Rails Setup |
| 2 | + |
| 3 | +*Super concise, copy-paste instructions for AI agents to set up React on Rails in common scenarios.* |
| 4 | + |
| 5 | +## 🔍 **Before Starting: Check Current Versions** |
| 6 | + |
| 7 | +```bash |
| 8 | +# Get latest available versions (recommended approach) |
| 9 | +gem search react_on_rails --remote |
| 10 | +gem search shakapacker --remote |
| 11 | + |
| 12 | +# Or use specific versions from these commands in your Gemfile: |
| 13 | +# Latest stable versions as of Jan 2025: |
| 14 | +# react_on_rails ~> 14.2 |
| 15 | +# shakapacker ~> 8.1 |
| 16 | +``` |
| 17 | + |
| 18 | +**⚠️ Version Flexibility:** These instructions use `~> X.Y` which allows patch updates. Always check for latest versions before starting a new project. |
| 19 | + |
| 20 | +--- |
| 21 | + |
| 22 | +## 🆕 Scenario 1: New Rails App with React on Rails |
| 23 | + |
| 24 | +```bash |
| 25 | +# Create new Rails app with essential gems |
| 26 | +rails new myapp --skip-javascript --database=postgresql |
| 27 | +cd myapp |
| 28 | + |
| 29 | +# Add React on Rails to Gemfile (latest versions) |
| 30 | +echo 'gem "react_on_rails", "~> 14.2"' >> Gemfile |
| 31 | +echo 'gem "shakapacker", "~> 8.1"' >> Gemfile |
| 32 | +bundle install |
| 33 | + |
| 34 | +# Install React on Rails with Node dependencies |
| 35 | +rails generate react_on_rails:install |
| 36 | +yarn install |
| 37 | + |
| 38 | +# Start development servers |
| 39 | +bin/dev |
| 40 | +``` |
| 41 | + |
| 42 | +**✅ Success Check:** Visit `http://localhost:3000/hello_world` → Should see "Hello World" from React |
| 43 | + |
| 44 | +**📁 Generated Files:** |
| 45 | +- `app/javascript/bundles/HelloWorld/components/HelloWorld.jsx` |
| 46 | +- `app/controllers/hello_world_controller.rb` |
| 47 | +- `app/views/hello_world/index.html.erb` |
| 48 | + |
| 49 | +--- |
| 50 | + |
| 51 | +## 🔄 Scenario 2: Add React on Rails to Existing Rails App |
| 52 | + |
| 53 | +```bash |
| 54 | +# Navigate to existing Rails app root |
| 55 | +cd /path/to/existing/app |
| 56 | + |
| 57 | +# Add gems to Gemfile (before final 'end') |
| 58 | +cat >> Gemfile << 'EOF' |
| 59 | +
|
| 60 | +# React on Rails |
| 61 | +gem "react_on_rails", "~> 14.2" |
| 62 | +gem "shakapacker", "~> 8.1" |
| 63 | +EOF |
| 64 | + |
| 65 | +# Install gems |
| 66 | +bundle install |
| 67 | + |
| 68 | +# Install React on Rails (will not overwrite existing files) |
| 69 | +rails generate react_on_rails:install --ignore-existing-files |
| 70 | + |
| 71 | +# Install Node dependencies |
| 72 | +yarn install |
| 73 | + |
| 74 | +# Add React component to existing view |
| 75 | +# Replace <view-name> with your actual view file |
| 76 | +cat >> app/views/<view-name>/<action>.html.erb << 'EOF' |
| 77 | +
|
| 78 | +<%= react_component("HelloWorld", props: { name: "World" }) %> |
| 79 | +EOF |
| 80 | + |
| 81 | +# Start development |
| 82 | +bin/dev |
| 83 | +``` |
| 84 | + |
| 85 | +**⚠️ Pre-flight Checks:** |
| 86 | +- Rails app has `bin/dev` or similar dev script |
| 87 | +- `package.json` exists (if not, run `yarn init -y` first) |
| 88 | +- No existing React setup conflicts |
| 89 | + |
| 90 | +**✅ Success Check:** React component renders in your chosen view |
| 91 | + |
| 92 | +--- |
| 93 | + |
| 94 | +## ⚡ Scenario 3: Convert Vite-Ruby to React on Rails |
| 95 | + |
| 96 | +```bash |
| 97 | +# Navigate to app root |
| 98 | +cd /path/to/vite/ruby/app |
| 99 | + |
| 100 | +# Remove Vite-Ruby gems from Gemfile |
| 101 | +sed -i.bak '/gem.*vite_rails/d' Gemfile |
| 102 | +sed -i.bak '/gem.*vite_ruby/d' Gemfile |
| 103 | + |
| 104 | +# Add React on Rails gems to Gemfile |
| 105 | +cat >> Gemfile << 'EOF' |
| 106 | +
|
| 107 | +# React on Rails (replacing Vite) |
| 108 | +gem "react_on_rails", "~> 14.2" |
| 109 | +gem "shakapacker", "~> 8.1" |
| 110 | +EOF |
| 111 | + |
| 112 | +# Install new gems |
| 113 | +bundle install |
| 114 | + |
| 115 | +# Backup existing Vite config |
| 116 | +mv vite.config.* vite.config.backup 2>/dev/null || true |
| 117 | + |
| 118 | +# Remove Vite-specific files |
| 119 | +rm -rf config/vite.json |
| 120 | +rm -rf bin/vite* |
| 121 | + |
| 122 | +# Install React on Rails |
| 123 | +rails generate react_on_rails:install --force |
| 124 | + |
| 125 | +# Migrate existing React components |
| 126 | +# Move components from app/frontend/entrypoints/ to app/javascript/bundles/ |
| 127 | +mkdir -p app/javascript/bundles/Components |
| 128 | +find app/frontend -name "*.jsx" -o -name "*.tsx" | while read file; do |
| 129 | + basename=$(basename "$file") |
| 130 | + cp "$file" "app/javascript/bundles/Components/$basename" |
| 131 | +done |
| 132 | + |
| 133 | +# Update component registrations in app/javascript/packs/hello-world-bundle.js |
| 134 | +echo "// Register your existing components here" |
| 135 | +echo "// import YourComponent from '../bundles/Components/YourComponent';" |
| 136 | +echo "// ReactOnRails.register({ YourComponent });" |
| 137 | + |
| 138 | +# Clean up old Vite files |
| 139 | +rm -rf app/frontend |
| 140 | +rm -rf public/vite* |
| 141 | + |
| 142 | +# Update views to use React on Rails helpers |
| 143 | +# Replace vite_javascript_tag with javascript_pack_tag |
| 144 | +# Replace vite_stylesheet_tag with stylesheet_pack_tag |
| 145 | + |
| 146 | +# Install dependencies |
| 147 | +yarn install |
| 148 | + |
| 149 | +# Start development |
| 150 | +bin/dev |
| 151 | +``` |
| 152 | + |
| 153 | +**🔧 Manual Steps Required:** |
| 154 | +1. **Update views**: Replace `vite_javascript_tag` with `javascript_pack_tag "hello-world-bundle"` |
| 155 | +2. **Register components**: Add your components to `app/javascript/packs/hello-world-bundle.js` |
| 156 | +3. **Update imports**: Change relative paths if needed |
| 157 | + |
| 158 | +**✅ Success Check:** |
| 159 | +- `bin/dev` starts without Vite errors |
| 160 | +- React components render using `<%= react_component("YourComponent") %>` |
| 161 | + |
| 162 | +--- |
| 163 | + |
| 164 | +## 🛠️ Common Troubleshooting Commands |
| 165 | + |
| 166 | +```bash |
| 167 | +# Check current versions and compatibility |
| 168 | +bundle info react_on_rails shakapacker |
| 169 | +rails --version |
| 170 | +ruby --version |
| 171 | +node --version |
| 172 | + |
| 173 | +# Check React on Rails installation |
| 174 | +rails runner "puts ReactOnRails::VERSION" |
| 175 | + |
| 176 | +# Verify Shakapacker setup |
| 177 | +bin/shakapacker --version |
| 178 | + |
| 179 | +# Clear cache if components not updating |
| 180 | +rm -rf tmp/cache public/packs |
| 181 | +rails assets:clobber |
| 182 | + |
| 183 | +# Check component registration |
| 184 | +rails runner "puts ReactOnRails.configuration.components_subdirectory" |
| 185 | + |
| 186 | +# Restart with clean build |
| 187 | +pkill -f "bin/shakapacker-dev-server" |
| 188 | +rm -rf public/packs-test |
| 189 | +bin/dev |
| 190 | +``` |
| 191 | + |
| 192 | +--- |
| 193 | + |
| 194 | +## 📋 Quick Reference |
| 195 | + |
| 196 | +### Essential Files Structure |
| 197 | +``` |
| 198 | +app/ |
| 199 | +├── controllers/hello_world_controller.rb |
| 200 | +├── views/hello_world/index.html.erb |
| 201 | +└── javascript/ |
| 202 | + ├── bundles/HelloWorld/components/HelloWorld.jsx |
| 203 | + └── packs/hello-world-bundle.js |
| 204 | +``` |
| 205 | + |
| 206 | +### Key Commands |
| 207 | +- **Development**: `bin/dev` (starts Rails + Shakapacker) |
| 208 | +- **Generate**: `rails generate react_on_rails:install` |
| 209 | +- **Component**: `<%= react_component("ComponentName", props: {}) %>` |
| 210 | + |
| 211 | +### Version Requirements |
| 212 | +- Rails 7+ (Rails 8 supported), Ruby 3.0+ (Ruby 3.2+ for Rails 8), Node 20+ LTS, Yarn |
| 213 | +- react_on_rails ~> 14.2+, shakapacker ~> 8.1+ |
| 214 | +- **Note**: Use `bundle info react_on_rails` to check latest available version |
| 215 | + |
| 216 | +--- |
| 217 | + |
| 218 | +*💡 **Pro Tip for AI Agents**: Always run `bin/dev` to test setup, and check browser console for any JavaScript errors.* |
0 commit comments