# Redirect after Turbo stream response

**URL:** https://discuss.hotwired.dev/t/redirect-after-turbo-stream-response/2303
**Category:** General
**Created:** [February 15, 2021, 1:24am UTC](https://discuss.hotwired.dev/t/redirect-after-turbo-stream-response/2303 "2021-02-15T01:24:51Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![feute](https://yyz1.discourse-cdn.com/flex027/user_avatar/discuss.hotwired.dev/feute/32/1640_2.png) [@feute](https://discuss.hotwired.dev/u/feute)
#### Post date: [February 15, 2021, 1:24am UTC](https://discuss.hotwired.dev/t/redirect-after-turbo-stream-response/2303/1 "2021-02-15T01:24:51Z")

</div>

Hello. I was wondering if there’s a way to perform a redirect after returning a Turbo stream response in Rails.

For instance, in the chat app from the Hotwire screencast, they say that one can use a Stimulus controller to _clear_ the message form field, but if we have form errors we would have to clear those too; the new message frame kind of keeps unmodified, without a render or redirection.

The following code from the chat app makes it think that it could return either a stream _or_ a redirect when successfully saving the message model, but not both:

```
respond_to do |format|
  if @message.save
    format.turbo_stream # maybe something here to respond with a render/redirect too?
    format.html { redirect_to @room }
  else
    format.html { render :new, status: :unprocessable_entity }
  end
end

```

One use case I can think of is to _clear_ the form and its errors by redirecting the frame to the same path (this would avoid the need to create Stimulus controllers to clear form fields/errors). Another use case is to render a “New comment” button which replaces its frame to the comment’s form when clicked, and redirect back to the new comment button when the form submission is successful (apart from using Turbo streams to broadcast the new comment and rendering it in another frame, like the chat app example).

My workaround to this is to save the redirect path as a data attribute in the DOM and create a Stimulus controller to redirect using `Turbo.visit`, like so:

```
import { Controller } from "stimulus";
import { Turbo } from "@hotwired/turbo-rails";

export default class extends Controller {
  formRedirect(event) {
    const redirectPath = this.element.dataset.redirect;

    if (event.detail.success) {
      Turbo.visit(redirectPath);
    }
  }
}

```

But what I would want is not to rely on custom JavaScript code, and for Turbo[-rails] to be able to return both a stream and render/redirect.

---

<div class="post-metadata">

### Author: ![feute](https://yyz1.discourse-cdn.com/flex027/user_avatar/discuss.hotwired.dev/feute/32/1640_2.png) [@feute](https://discuss.hotwired.dev/u/feute)
#### Post date: [February 15, 2021, 1:57am UTC](https://discuss.hotwired.dev/t/redirect-after-turbo-stream-response/2303/2 "2021-02-15T01:57:35Z")

</div>

I think I just found the solution. It seems that it’s as simple as redirecting _inside_ the `turbo_stream` block:

```
format.turbo_stream { redirect_to article_path(@article) }

```

Maybe I just did something wrong before because I tried it and it didn’t quite work.

I just need to confirm if this is the desired behaviour.

---

<div class="post-metadata">

### Author: ![thomasvanholder](https://yyz1.discourse-cdn.com/flex027/user_avatar/discuss.hotwired.dev/thomasvanholder/32/1538_2.png) [@thomasvanholder](https://discuss.hotwired.dev/u/thomasvanholder)
#### Post date: [May 14, 2021, 10:09am UTC](https://discuss.hotwired.dev/t/redirect-after-turbo-stream-response/2303/3 "2021-05-14T10:09:22Z")

</div>

I’ve had a similar use case. A flash message (turbo\_stream.erb) needs to be briefly shown inside a modal. Afterward, the page has to be redirected.

A solution for me was to add a data-controller=“redirect” and set the URL value on the wrapping div from flash message. The path is being passed as a local from the turbo\_stream.erb file.

**flash.html.erb**

```auto
<div data-controller="redirect" data-redirect-url-value="<%= path %>">
  <%= message %>
</div>

```

**redirect\_controller.js**

```auto
import { Controller } from 'stimulus'

export default class extends Controller {
  static values = { url: String }
  connect () {
    window.location.href = this.urlValue
  }
}

```

---

<div class="post-metadata">

### Author: ![Tenzin\_Dorjee](https://yyz1.discourse-cdn.com/flex027/user_avatar/discuss.hotwired.dev/tenzin_dorjee/32/2000_2.png) [@Tenzin\_Dorjee](https://discuss.hotwired.dev/u/Tenzin_Dorjee)
#### Post date: [May 25, 2021, 6:41am UTC](https://discuss.hotwired.dev/t/redirect-after-turbo-stream-response/2303/4 "2021-05-25T06:41:18Z")

</div>

It works(loads the whole page of the path we provided), thanks @thomasvanholder . But not sure how do we load only the turbo frame of the path we provided.

---

<div class="post-metadata">

### Author: ![nayaabkhan](https://yyz1.discourse-cdn.com/flex027/user_avatar/discuss.hotwired.dev/nayaabkhan/32/2216_2.png) [@nayaabkhan](https://discuss.hotwired.dev/u/nayaabkhan)
#### Post date: [September 15, 2021, 4:56am UTC](https://discuss.hotwired.dev/t/redirect-after-turbo-stream-response/2303/5 "2021-09-15T04:56:42Z")

</div>

@Tenzin_Dorjee Agreed! That reloads the whole page. But it would be great to make it just go back to the previous frame.

---

<div class="post-metadata">

### Author: ![Kjell](https://yyz1.discourse-cdn.com/flex027/user_avatar/discuss.hotwired.dev/kjell/32/2868_2.png) [@Kjell](https://discuss.hotwired.dev/u/Kjell)
#### Post date: [August 23, 2022, 2:44pm UTC](https://discuss.hotwired.dev/t/redirect-after-turbo-stream-response/2303/6 "2022-08-23T14:44:57Z")

</div>

Here is a working demonstration of the solution suggested by @thomasvanholder :

> **[GitHub - KjellMorgenstern/turbo-todos: Adding a turbo redirect for todo/show...](https://github.com/KjellMorgenstern/turbo-todos)**
>
> Adding a turbo redirect for todo/show action when completed - GitHub - KjellMorgenstern/turbo-todos: Adding a turbo redirect for todo/show action when completed

Todo details (show) redirects to the index page when a todo is marked completed. Even if it happens in the backend or another browser window.

---

<div class="post-metadata">

### Author: ![ratanachai](https://yyz1.discourse-cdn.com/flex027/user_avatar/discuss.hotwired.dev/ratanachai/32/3187_2.png) [@ratanachai](https://discuss.hotwired.dev/u/ratanachai)
#### Post date: [March 20, 2023, 5:19am UTC](https://discuss.hotwired.dev/t/redirect-after-turbo-stream-response/2303/7 "2023-03-20T05:19:31Z")

</div>

Thank you @thomasvanholder for the solution. It worked without any problems. However, can anyone tell me why do we need the line below to work?

```auto
  static values = { url: String }

```

Also this one as well… (I can guess but where can I read more about this convention?) Thanks

```auto
data-redirect-url-value="<%= path %>"

```

---

<div class="post-metadata">

### Author: ![mgodwin](https://yyz1.discourse-cdn.com/flex027/user_avatar/discuss.hotwired.dev/mgodwin/32/228_2.png) [@mgodwin](https://discuss.hotwired.dev/u/mgodwin)
#### Post date: [June 27, 2023, 4:10am UTC](https://discuss.hotwired.dev/t/redirect-after-turbo-stream-response/2303/8 "2023-06-27T04:10:18Z")

</div>

Hey I think you’re looking for the docs on Stimulus Values: [Stimulus Reference](https://stimulus.hotwired.dev/reference/values#definitions)

Hope that helps!

---

<div class="post-metadata">

### Author: ![mhenrixon](https://yyz1.discourse-cdn.com/flex027/user_avatar/discuss.hotwired.dev/mhenrixon/32/2217_2.png) [@mhenrixon](https://discuss.hotwired.dev/u/mhenrixon)
#### Post date: [August 9, 2024, 10:15am UTC](https://discuss.hotwired.dev/t/redirect-after-turbo-stream-response/2303/9 "2024-08-09T10:15:32Z")

</div>

The solution while retarded is pretty simple to this problem.

For a `turbo_stream.erb` response, you want the `flash.now[:alert]` way but for a redirect you need to set the `flash[:alert]` before redirecting. See the below code for a working example:

```ruby
  def create
    authorize! :create, current_company.sell_orders.new

    @sell_order = current_company.sell_orders.new(sell_order_params.compact_blank)
    @out_car = current_company.cars.includes(:branch).find(params[:out_car_id])

    if params[:trade]
      trade_regnr = trade_params[:regnr].upcase
      if (trade = Car.find_or_create_by_regnr(trade_regnr))
        trade.update!(trade_params)
      else
        respond_to do |format|
          format.turbo_stream do
            flash.now[:alert] = t("flash.trade_car_not_found", regnr: trade_regnr)
            render :new, status: :unprocessable_entity, alert: alert_message
          end
        end
      end 
    end

    if @sell_order.save
      respond_to do |format|
        format.turbo_stream do
          flash[:success] = t("flash.sell_order_created")
          redirect_to [:edit, @sell_order], status: :see_other
        end
      end
    else
      respond_to do |format|
        format.turbo_stream do
          flash.now[:alert] = t("flash.actions.create.alert", resource_name: "Säljorder")
          render :new, status: :unprocessable_entity
        end
      end
    end
  end

```

---

<div class="post-metadata">

### Author: ![9sim9](https://avatars.discourse-cdn.com/v4/letter/9/8dc957/32.png) [@9sim9](https://discuss.hotwired.dev/u/9sim9)
#### Post date: [January 17, 2025, 2:12am UTC](https://discuss.hotwired.dev/t/redirect-after-turbo-stream-response/2303/10 "2025-01-17T02:12:45Z")

</div>

For anyone who still needs it this is the solution…

```auto
respond_to do |format|
  format.turbo_stream { redirect_to something_path(format: :html) }
end

```
