Skip to content

Instantly share code, notes, and snippets.

@julianrubisch
Last active November 9, 2020 14:34
Show Gist options
  • Save julianrubisch/29ad4a795251e590a4ef34240adc0115 to your computer and use it in GitHub Desktop.
Save julianrubisch/29ad4a795251e590a4ef34240adc0115 to your computer and use it in GitHub Desktop.
StimulusReflex Selector Morph
<%= article.title # ... etc %>
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
morph "##{resource_key}", ApplicationController.render(resource.underscore, collection: resource.constantize.where(session[resource_key][:param] => session[resource_key][: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 'article', collection: @articles %>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment