Skip to main content

organizing_helpers_with_concern

🔧 Organizing Helpers with ActiveSupport::Concern Across Controllers and Views​

Extract shared logic into an ActiveSupport::Concern so it’s available in controllers (via helper_method) and views alike. This promotes single‑source‑of‑truth for behavior used in multiple layers.

# app/controllers/concerns/taggable.rb
module Taggable
extend ActiveSupport::Concern

included do
helper_method :tag_cloud
end

def tag_cloud(tags)
tags.map { |t| link_to t.name, tag_path(t), class: 'tag' }.join(' ').html_safe
end
end

# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
include Taggable
end

Now you can call tag_cloud(@tags) from controllers or directly in views.