Skip to content

Instantly share code, notes, and snippets.

@wrburgess
Created January 9, 2022 21:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wrburgess/452b55c073ee16f57a1250b5b1a92954 to your computer and use it in GitHub Desktop.
Save wrburgess/452b55c073ee16f57a1250b5b1a92954 to your computer and use it in GitHub Desktop.
Rails stimulus per-page or per-view controller actions. keywords: javascript, ruby, rails, stimulus, esbuild
<div
class="container-fluid admin <%= params[:controller] %> <%= params[:action] %>"
data-controller="<%= obj.model_name.plural.downcase %>"
data-<%= obj.model_name.plural.downcase %>-action="<%= params[:action] %>"
>
</div>
<div class="container-fluid admin widgets new" data-controller="widgets" data-widgets-action="new">
</div>
import { Controller } from '@hotwired/stimulus'
const newConnect = function() {
console.log('action:', 'new', 'connected')
}
const editConnect = function() {
console.log('action:', 'new', 'connected')
}
const showConnect = function() {
console.log('action:', 'new', 'connected')
}
const indexConnect = function() {
console.log('action:', 'new', 'connected')
}
export default class extends Controller {
connect() {
const controllerAction = this.data.get('action')
switch (controllerAction) {
case 'edit':
editConnect()
break
case 'new':
newConnect()
break
case 'show':
showConnect()
break
case 'index':
indexConnect()
break
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment