Lesson 5 — Rails helpers in Phlex
Rails view helpers — link_to, image_tag, form_with, route helpers — work in Phlex through an adapter system. Adapters are not included by default, which keeps Phlex components lean.
Routes helpers
Routes are the most commonly needed helpers. Include them in Components::Base and Views::Base so they are available everywhere:
|
|
The generator already does this — but it is worth understanding why. With routes included, you can use path helpers directly:
|
|
Do you need link_to?
The Phlex docs make a good point: do you really need link_to when you have the a tag? In most cases, no:
|
|
The a tag with a route helper is cleaner in Phlex than link_to. You only need the link_to adapter if you are porting existing code that uses it heavily.
Including other adapters
If you need a specific helper, include its adapter. The module name matches the helper name in TitleCase:
|
|
Tip: If you call a helper you haven’t included, Phlex gives you a helpful error message that tells you exactly which adapter to include. You don’t need to know the full list upfront — just add adapters as you need them.
CSRF and meta tags
These are needed in the layout head. Include their adapters in Components::Layouts::AppLayout or in Components::Base:
|
|
Then in the layout:
|
|
Custom helpers
Rails application helpers live in app/helpers/ and are automatically
available in ERB views. In Phlex they are not automatically available —
you register them explicitly on your base class.
A typical example is current_user. In a standard Rails app with
authentication, current_user is defined in ApplicationController and
made available to views as a helper method:
|
|
To make current_user available inside Phlex components and views,
register it on Components::Base:
|
|
Now you can call current_user directly inside any component or view:
|
|
For helpers defined in app/helpers/application_helper.rb:
|
|
Register it as a value helper since it returns a value rather than outputting HTML directly:
|
|
Then use it directly in any component or view:
|
|
Output helpers are for methods that generate and push HTML directly to the buffer — like pagination helpers from gems such as Pagy:
|
|
The distinction matters: output helpers push their return value directly to the buffer, while value helpers return a value you use in an expression. Getting this wrong produces either double-rendered output or escaped HTML strings appearing as visible text on the page.
Never
includeRails helper modules directly into a Phlex component. Rails helper modules are not designed to work with Phlex and may override core Phlex methods likecapture, causing subtle and hard-to-diagnose bugs. Always useregister_value_helperorregister_output_helperinstead.
We don’t have authentication wired up yet — that comes in Module 10. So
current_user won’t work as shown until then. But establishing the
pattern now means when authentication arrives, you know exactly where to
register it.
Date and time formats
Formatting dates consistently across an app is a common need. The instinct is to reach for a helper method — but Ruby already has a clean built-in solution that needs no helper registration at all.
Date::DATE_FORMATS and Time::DATE_FORMATS are Ruby hashes you can
extend in an initializer to define named formats. Once defined, call them
anywhere with to_s(:format_name) — in components, views, models, or
the console:
|
|
Usage in a Phlex component — no helper adapter, no registration, just
to_fs with a named format:
|
|
Define your app’s full set of formats in
config/initializers/date_formats.rb at the start of the project.
Consistent naming means to_fs(:short) always means the same thing
everywhere, and changing a format means changing one line.
What about
I18n.localize? Rails also ships withI18n.localize(aliased aslin views) which reads formats from locale YAML files. This is the right choice if you need multi-locale support — different date formats for different languages. For a single-locale app like KanbanFlow,DATE_FORMATSis simpler and sufficient. If you do needlin a Phlex component, register it as a value helper:register_value_helper :l.
The helpers proxy
For any helper that doesn’t have an adapter and you haven’t registered, access it through the helpers proxy. This returns the value rather than outputting it:
|
|
In practice, reaching for helpers is a code smell — there’s almost always a cleaner Phlex alternative.
Exercise
Set up the app’s date and time formats. Create the initializer and add some defaults:
Then add a footer to AppLayout that uses them:
|
|
Now update the body in the view-template of AppLayout to use the new footer (we’re also doing a bit of css change to force the footer to the bottom of the page).
|
|
Restart the server so the initializer is picked up, then visit http://localhost:3000. You should see the footer with the current date and time formatted using your named format.
Verify the formats work as expected in the Rails console:
|
|