Getting started with Phlex on Rails
While Phlex can be used in any Ruby project, it’s especially great with Rails. But before we get into the details, it’s important to understand that Phlex is very different from ActionView and ViewComponent.
Setup
To use Phlex with Rails, you’ll need to install the phlex-rails
gem. Add the following to your Gemfile and run bundle install
.
gem "phlex-rails"
Note, you do not need to install phlex
separately because phlex
is a dependency of phlex-rails
.
Once the gem is installed, run the install generator.
bin/rails generate phlex:install
This script will:
- update
config/application.rb
to includeapp/views
,app/views/components
, andapp/views/layouts
in your auto-load paths; - generate
views/application_view.rb
- generate
views/layouts/application_layout.rb
- generate
views/components/application_component.rb
ApplicationComponent
is your base component which all your other components inherit from. By default, ApplicationView
inherits from ApplicationComponent
.