Skip to content

awesome/rails6-mongoid-stimulus

Repository files navigation

rails6-mongoid-stimulus

Example Rails 6.1 app with Mongoid & Stimulus (no rails_ujs)

Why?

Pretty much to address removal of rails_ujs breaking Devise's "The default HTTP method used to sign out a resource. Default is :delete".

About this repo

Features

Stimulus

link_to-controller

NOTICE: Functionality ported from rails_ujs is scoped to link_to here in rails6-mongoid-stimulus

Usage
  1. Install Stimulus for Rails.

  2. Add link-to_controller.js to app/javascripts/controllers/.

  3. In view template, use link_to per instructions and documentation.

  4. To link_to add stimulus controller and action with data attributes like: data: {controller: 'link-to', action: 'click->link-to#method'}

Example:

= link_to "delete", "/delete", method: :delete, data: {controller: 'link-to', action: 'click->link-to#method'}
%br
= link_to "delete", "/delete", method: :delete, data: {controller: 'link-to', action: 'click->link-to#method', confirm: 'Are you sure?'}

HAML
# 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>
ERB
# 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>

Links about stimulus vs rails_ujs

Mongoid

  • Gemfile: gem 'mongoid', github: 'mongodb/mongoid', branch: '7.2-stable'
    gem 'mongoid', github: 'mongodb/mongoid', branch: '7.2-stable'
  • 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

MIT License