Skip to content

Instantly share code, notes, and snippets.

@julianrubisch
Last active October 4, 2020 02:06
Show Gist options
  • Save julianrubisch/1be39bbd122e7a2a5525cf4611dccc10 to your computer and use it in GitHub Desktop.
Save julianrubisch/1be39bbd122e7a2a5525cf4611dccc10 to your computer and use it in GitHub Desktop.
StimulusReflex Concern-Like Architecture
class ArticlesController < ApplicationController
def index
@articles = Article.where(session[:articles][:param] => session[:articles][:value])
end
end
class FilterableReflex < ApplicationReflex
def filter
resource, param = element.dataset.to_h.fetch_values(:resource, :param)
resource_key = resource.pluralize.underscore.to_sym
session[resource_key][:param] = param
session[resource_key][:value] = element.value
end
end
<%= label_tag "category" %>
<%= select_tag "category",
options_from_collection_for_select(Category.all, :id, :name)
data: { action: "change->filterable#filter",
resource: 'Article',
param: 'category',
reflex_root: "#articles" } %>
<div id="articles">
<%= render @articles %>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment