Home > front end >  ActiveAdmin / Formtastic - How to have an input that is not associated to the model?
ActiveAdmin / Formtastic - How to have an input that is not associated to the model?

Time:11-14

I'm using activeadmin and in a form associated with a model I would need to add a checkbox that is not tied to any of the model attributes. It should be just a checkbox that won't allow to submit the form until it's checked - I'll also add some javascript to achieve this.

I've checked the documentation of ActiveAdmin and Formtastic but I wan't able to find anything for this purpose. How to add the custom checkbox and where I should add the javascript functionality?

CodePudding user response:

There’s a couple ways to achieve this, but you’ll likely want to submit it as a param as well.

If it’s implemented using simply JS the end user can get around its necessity for submission, while your backend could filter for such submissions.

Though you may not particularly care about this.

One of my favourite tools for JS scripts is AlpineJS and is how I’d go about implementing a JS version for this (and how I’ve done similar implementations in the past).

By tying the value of a checkbox to a AlpineJS variable you can then use x-bind to toggle classes on the submission buttons for disabling/enabling.

CodePudding user response:

In this specific case (Having a "Are you sure?" checkbox) the solution was using Rails' Acceptance validation which makes sure that the user ticked a checkbox when the form was submitted.

Even if it's working, I'm still looking for a way to manually edit the form template HTML (e.g. to add some extra HTML tags).

  • Related