# 1-click unsubscribe - using stimulusjs to auto-submit a link\_to ... method: :post

**URL:** <https://discuss.hotwired.dev/t/1-click-unsubscribe-using-stimulusjs-to-auto-submit-a-link-to-method-post/1476>\
**Category:** General\
**Created:** [December 2, 2020, 9:20pm UTC](https://discuss.hotwired.dev/t/1-click-unsubscribe-using-stimulusjs-to-auto-submit-a-link-to-method-post/1476 "2020-12-02T21:20:51Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![andreimoment](https://yyz1.discourse-cdn.com/flex027/user_avatar/discuss.hotwired.dev/andreimoment/32/486_2.png) [@andreimoment](https://discuss.hotwired.dev/u/andreimoment)\
**Post date:** [December 2, 2020, 9:20pm UTC](https://discuss.hotwired.dev/t/1-click-unsubscribe-using-stimulusjs-to-auto-submit-a-link-to-method-post/1476/1 "2020-12-02T21:20:51Z")

</div>

Wondering what would be a good way to use stimulus with to submit the form autogenerated by a link\_to with a method: :post.

The use case is a 1-click unsubscribe: click on unsubscription link in email footer -\> “new” unsubscription page, auto-redirect via post to the “update” action.

I considered updating the subscription directly via the link but triggering changes via “get” goes against the protocol of HTTP verbs and could be triggered by browser prefetch.

So, the stimulus controller will check if the request is made by a browser, is visible in an active tab, and if yes, will redirect to the update action. Otherwise, it will fall back on the “new” page with a link to confirm the unsubscription.

The code in unsubscriptions/new.slim:

`= link_to "Confirm unsubscription", confirm_unsubscription_path(email_address: params[:email_address]), method: :post`

When given :post, the Rails link\_to helper creates a form. Ideally the stimulus controller would post that form. In a less attractive scenario I’d have to manually create a form everywhere I use such a flow.

Is there a way for me to use a stimulus controller on the link\_to helper and initiate the post action?

---

<div class="post-metadata">

**Author:** ![andreimoment](https://yyz1.discourse-cdn.com/flex027/user_avatar/discuss.hotwired.dev/andreimoment/32/486_2.png) [@andreimoment](https://discuss.hotwired.dev/u/andreimoment)\
**Post date:** [December 3, 2020, 12:20am UTC](https://discuss.hotwired.dev/t/1-click-unsubscribe-using-stimulusjs-to-auto-submit-a-link-to-method-post/1476/2 "2020-12-03T00:20:08Z")

</div>

Answering my own question - this was simpler than I expected. The target data element is on the `link_to` helper.

If the visibility api is unsupported or the browser is pre-fetching, this will fall back to showing the page with the link. Exactly what I needed.

```
export default class extends Controller {
  static targets = ["item"]

  connect() {
   // console.log(this.itemTarget)

    if ( document.visibilityState == 'visible') {
      this.itemTarget.click()
    }
  }

}
```
