Link_to render div in the same page

i have this link_to inside my table
<%= link_to(orders_outbound_path(order, format: :turbo_stream)) { render_icon 'pencil', class: "h-5 w-5 inline-block", data: { turbo_frame: "_top" } } %>

That is because format: :turbo_stream is not how you tell the controller to recognize turbo-stream response you need to add turbo-stream as the content type. You can use the data-turbo-method attribute on the link and set it to post then instead of a show action do a create action and render the turbo-stream from there.

While what @yunggindigo writes is correct. You can also use @rails/requestjs:

Something like this could work:

import { Controller } from "@hotwired/stimulus"
import hotkeys from "hotkeys-js"
import { FetchRequest } from "@rails/request.js"

export default class Hotkey extends Controller {
  static targets = ["modal"]
  static values = {
    url: String
  }

  connect() {
    hotkeys("⌘+k", (event, handler) => {
      this.showCommands()
    })
  }

  async showCommands() {
    const request = new FetchRequest("get", this.urlValue, { responseKind: "turbo-stream" })
    const response = await request.perform()
    if (response.ok) {
      const result = await response.html

      this.modalTarget.insertAdjacentHTML("beforebegin", result)
    }
  }

  hideCommands() {
    this.modalTarget.close()
  }
}
<body data-controller="hotkey" data-hotkey-url-value="<%= commands_path %>" data-action="keyup@document->modal#escClose">
    <%= turbo_frame_tag :commands, data: { hotkey_target: "modal" } %>
</body>
1 Like