Ruby on Rails 8 introduces several improvements that make development easier, more secure, and more maintainable. In this guide, we’ll walk through setting up a new Rails 8 application while noting the significant configurations and features that come out of the box.
1. Check Your Ruby and Rails Versions
If not installed Ruby 3.4 and Rails 8.0 please check: https://railsdrop.com/2025/02/11/installing-and-setup-ruby-3-rails-8-vscode-ide-on-macos-in-2025/
Before starting, ensure that you have the correct versions of Ruby and Rails installed:
$ ruby -v
ruby 3.4.1
$ rails -v
Rails 8.0.1
If you donโt have these versions installed, update them using your package manager or version manager (like rbenv or RVM).
2. Create a New Rails 8 Application
Run the following command to create a new Rails app:
$ rails new design_studio
Noteworthy Files and Directories Created
Here are some interesting files and directories that are generated with a new Rails 8 app:
create .ruby-version
create bin/brakeman
create bin/rubocop
create bin/docker-entrypoint
create .rubocop.yml
create .github/workflows
create .github/workflows/ci.yml
create config/cable.yml
create config/storage.yml
create config/initializers/content_security_policy.rb
create config/initializers/filter_parameter_logging.rb
create config/initializers/new_framework_defaults_8_0.rb
Key Takeaways:
- Security & Code Quality Tools: Brakeman (security scanner) and RuboCop (code style linter) are included by default.
- Docker Support: The presence of
bin/docker-entrypointsuggests better built-in support for containerized deployment. - GitHub Actions Workflow: The
.github/workflows/ci.ymlfile provides default CI configurations. - Enhanced Security: The
content_security_policy.rbinitializer helps enforce a strict security policy. - New Rails Defaults: The
new_framework_defaults_8_0.rbinitializer helps manage breaking changes in Rails 8.
Rails automatically creates the following during the creation of the rails new app.
a. Configuring Import Maps and Installing Turbo & Stimulus
Rails 8 still defaults to Import Maps for JavaScript package management, avoiding the need for Node.js and Webpack:
$ rails turbo:install stimulus:install
This creates the following files:
create config/importmap.rb
create app/javascript/controllers
create app/javascript/controllers/index.js
create app/javascript/controllers/hello_controller.js
append config/importmap.rb
Key Takeaways:
- Import Maps: Defined in
config/importmap.rb, allowing dependency management without npm. - Hotwired Support: Turbo and Stimulus are automatically configured for modern front-end development.
- Generated Controllers: Stimulus controllers are pre-configured inside
app/javascript/controllers/.
b. Deploying with Kamal
Kamal simplifies deployment with Docker and Kubernetes. Rails 8 includes built-in support:
$ bundle binstubs kamal
$ bundle exec kamal init
This results in:
Created .kamal/secrets file
Created sample hooks in .kamal/hooks
Key Takeaways:
- Automated Deployment Setup: Kamal provides easy-to-use deployment scripts.
- Secret Management: The
.kamal/secretsfile ensures secure handling of credentials. - Deployment Hooks: Custom hooks allow pre- and post-deployment scripts for automation.
c. Setting Up Caching and Queues with Solid Cache, Queue, and Cable
NOTE: Rails automatically creates this for you while creating the rails app.
Rails 8 includes Solid Cache, Solid Queue, and Solid Cable for enhanced performance and scalability:
$ rails solid_cache:install solid_queue:install solid_cable:install
This creates:
create config/cache.yml
create db/cache_schema.rb
create config/queue.yml
Key Takeaways:
- Caching Support:
config/cache.ymlmanages application-wide caching. - Database-Powered Queue System: Solid Queue simplifies background job management without requiring external dependencies like Sidekiq.
- Real-Time WebSockets: Solid Cable offers Action Cable improvements for real-time features.
3. Rails 8 Migration Enhancements
Rails 8 provides new shortcuts and syntax improvements for database migrations:
NOT NULL Constraints with ! Shortcut
You can impose NOT NULL constraints directly from the command line using !:
# Example for not null constraints:
โ rails generate model User name:string!
Type Modifiers in Migrations
Rails 8 allows passing commonly used type modifiers directly via the command line. These modifiers are enclosed in curly braces {} after the field type.
# Example for model generation:
โ rails generate model Product name:string description:text
# Example for passing modifiers:
โ rails generate migration AddDetailsToProducts 'price:decimal{5,2}' supplier:references{polymorphic}
Generating a Scaffold for the Product Model
Let’s generate a complete scaffold for our Product model:
โ rails generate scaffold product title:string! description:text category:string color:string 'size:string{10}' 'mrp:decimal{7,2}' 'discount:decimal{7,2}' 'rating:decimal{1,1}'
โ design_studio git:(main) โ rails -v
Rails 8.0.1
โ design_studio git:(main) โ ruby -v
ruby 3.4.1 (2024-12-25 revision 48d4efcb85) +PRISM [arm64-darwin24]
โ design_studio git:(main) โ rails generate scaffold product title:string! description:text category:string color:string 'size:string{10}' 'mrp:decimal{7,2}' 'discount:decimal{7,2}' 'rating:decimal{1,1}'
Using the Rails Resource Generator
The rails g resource command is a powerful way to generate models, controllers, migrations, and routes all in one go. This is particularly useful when setting up RESTful resources in a Rails application.
Basic Syntax
โ rails g resource product
This command creates the necessary files for a new resource, including:
- A model (
app/models/product.rb) - A migration file (
db/migrate/) - A controller (
app/controllers/product_controller.rb) - Routes in
config/routes.rb - A test file (
test/controllers/product_controller_test.rborspec/)
Example Usage
To generate a Post resource with attributes:
โ rails g resource Product title:string! description:text brand:references
This will:
- Create a
Productmodel withtitleanddescriptionattributes. - Add a
brand_idforeign key as a reference. - Apply a
NOT NULLconstraint ontitle(!shortcut). - Generate a corresponding migration file.
- Set up routes automatically (
resources :products).
Running the Migration
After generating a resource, apply the migration to update the database:
โ rails db:migrate
Difference Between resource and scaffold
Rails provides both rails g resource and rails g scaffold, but they serve different purposes:
| Feature | rails g resource | rails g scaffold |
|---|---|---|
| Generates a Model | โ | โ |
| Generates a Migration | โ | โ |
| Generates a Controller | โ (empty actions) | โ (full CRUD actions) |
| Generates Views (HTML/ERB) | โ | โ (index, show, new, edit, etc.) |
| Generates Routes | โ | โ |
| Generates Helper Files | โ | โ |
| Generates Tests | โ | โ |
rails g resourceis minimalโit generates only essential files without view templates. It’s useful when you want more control over how your views and controller actions are built.rails g scaffoldis more opinionated and generates full CRUD functionality with prebuilt forms and views, making it ideal for rapid prototyping.
If you need full CRUD functionality quickly, use scaffold. If you prefer a leaner setup with manual control over implementation, use resource.
Conclusion
Rails 8 significantly enhances the development experience with built-in security tools, CI/CD workflows, streamlined deployment via Kamal, and modern front-end support with Turbo & Stimulus. It also improves caching, background jobs, and real-time features with Solid tools.
These improvements make Rails 8 a robust framework for modern web applications, reducing the need for additional dependencies while keeping development efficient and secure.
Enjoy Rails! ๐