# How to return the user to the top of the page after a 422 :unprocessable\_entity response?

**URL:** <https://discuss.hotwired.dev/t/how-to-return-the-user-to-the-top-of-the-page-after-a-422-unprocessable-entity-response/2881>\
**Category:** General\
**Created:** [May 23, 2021, 2:51pm UTC](https://discuss.hotwired.dev/t/how-to-return-the-user-to-the-top-of-the-page-after-a-422-unprocessable-entity-response/2881 "2021-05-23T14:51:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nadidami](https://avatars.discourse-cdn.com/v4/letter/n/91b2a8/32.png) [@nadidami](https://discuss.hotwired.dev/u/nadidami)\
**Post date:** [May 23, 2021, 2:51pm UTC](https://discuss.hotwired.dev/t/how-to-return-the-user-to-the-top-of-the-page-after-a-422-unprocessable-entity-response/2881/1 "2021-05-23T14:51:21Z")

</div>

Some forms in my app take up a full page and any errors are returned as flash messages at the top of the page.

With turbolinks, submitting a form with errors results in the form re-rendering, with the user being taken back to the top of the page. However, with turbo, responding with a 422 doesn’t change the scroll position. Users will be left at the bottom of the page, perhaps not realising that the form has submitted and that there are errors they need to address.

Is there a way I can ensure the user is sent to the top of the page after a form re-renders with status 422?

Thank you!

---

<div class="post-metadata">

**Author:** ![dan](https://yyz1.discourse-cdn.com/flex027/user_avatar/discuss.hotwired.dev/dan/32/1529_2.png) [@dan](https://discuss.hotwired.dev/u/dan)\
**Post date:** [May 23, 2021, 3:34pm UTC](https://discuss.hotwired.dev/t/how-to-return-the-user-to-the-top-of-the-page-after-a-422-unprocessable-entity-response/2881/2 "2021-05-23T15:34:10Z")

</div>

I think you’d need to hook up a stimulus controller. Something like this (untested):

```auto
# new.html.erb
<form data-controller="window" data-action="turbo:submit-end->window#scrollToTop">

# window_controller.js
scrollToTop() {
  window.scrollTo(0, 0)
}

```

(You could maybe also move the `data-controller` attribute up to the body, so the window\_controller is always available.)

---

<div class="post-metadata">

**Author:** ![nadidami](https://avatars.discourse-cdn.com/v4/letter/n/91b2a8/32.png) [@nadidami](https://discuss.hotwired.dev/u/nadidami)\
**Post date:** [May 23, 2021, 5:25pm UTC](https://discuss.hotwired.dev/t/how-to-return-the-user-to-the-top-of-the-page-after-a-422-unprocessable-entity-response/2881/3 "2021-05-23T17:25:40Z")

</div>

Yay, this works! Thank you very much, dan! 😊
