Example Rails 6.1 app with Mongoid & Stimulus (no rails_ujs)
Pretty much to address removal of rails_ujs breaking Devise's "The default HTTP method used to sign out a resource. Default is :delete".
- rails_ujs/@rails/ujs completely removed in anticipation of official deprecation and replacement by https://github.com/hotwired/stimulus-rails.
- Includes a
link_to 'blah', blah_path, method: :delete
(with optionalconfirm: 'Are you sure?'
) via stimulus instead of rails_ujs. - HAML used instead of ERB for view templates.
NOTICE: Functionality ported from rails_ujs is scoped to link_to
here in rails6-mongoid-stimulus
- includes rails_ujs delete and confirm/delete
- includes functionality of rails_ujs confirm
- includes rails_ujs Rails.isCrossDomain helper
-
Install Stimulus for Rails.
-
Add
link-to_controller.js
toapp/javascripts/controllers/
. -
In view template, use
link_to
per instructions and documentation. -
To
link_to
add stimulus controller and action with data attributes like:data: {controller: 'link-to', action: 'click->link-to#method'}
Example:
rails6-mongoid-stimulus/app/views/home/index.html.haml
Lines 4 to 6 in a2b3f0c
# in app/views/blah/whatevs.html.haml
= link_to "delete", "/blah/1337", method: :delete, data: {controller: 'link-to', action: 'click->link-to#method'}
# <a data-controller="link-to" data-action="click->link-to#method" rel="nofollow" data-method="delete" href="/blah/1337">delete</a>
= link_to "delete", "/blah/1337", method: :delete, data: {controller: 'link-to', action: 'click->link-to#method', confirm: 'Are you sure?'}
# <a data-controller="link-to" data-action="click->link-to#method" data-confirm="Are you sure?" rel="nofollow" data-method="delete" href="/blah/1337">delete</a>
# in app/views/blah/whatevs.html.erb
<%= link_to "delete", "/blah/1337", method: :delete, data: {controller: 'link-to', action: 'click->link-to#method'} %>
# <a data-controller="link-to" data-action="click->link-to#method" rel="nofollow" data-method="delete" href="/blah/1337">delete</a>
<%= link_to "delete", "/blah/1337", method: :delete, data: {controller: 'link-to', action: 'click->link-to#method', confirm: 'Are you sure?'} %>
# <a data-controller="link-to" data-action="click->link-to#method" data-confirm="Are you sure?" rel="nofollow" data-method="delete" href="/blah/1337">delete</a>
- https://discuss.hotwire.dev/t/button-to-not-showing-the-data-confirm/2046
- hotwired/stimulus-rails#29
- Gemfile:
gem 'mongoid', github: 'mongodb/mongoid', branch: '7.2-stable'
rails6-mongoid-stimulus/Gemfile
Line 41 in d8a4b8e
rails g mongoid:config
Rails-6.1 bug work-around by patching gem locally, see: "MONGOID-5042 rails g mongoid:config is not working on Rails 6.1" mongodb/mongoid#4953