I’m trying to make a simple scenario here, in a RoR application. Changing from React to Hotwire. The scenario is:
A dropdown and a table. When the dropdown is selected, a new request is made to the backend, and the table data is populated by the response data. On React (and any other front-end framework, but I’ll use React as an example because of the application), this is done, in summary:
- On change on the select, triggering a function
- This function requests the backend, gets the result, and sets the state, for example, in options
- The table is populated by this options state. So every time this option state changes, the table value will change
I’m having a hard time doing it with Hotwire
I have:
admin/work_queue/index.html.erb
<%= turbo_frame_tag 'work_queue' do %>
<%= form_with url: admin_work_queue_path(:turbo_stream), method: :get, data: { controller: "work-queue", action: "change->work-queue#applyFilter" } do %>
<%= select_tag :company_id, options_from_collection_for_select(Company.active, :id, :name, params[:company_id]), include_blank: '', class: "form-select" %> <% end %>
<%= render partial: "admin/work_queue/work_queue_follow_up_table", locals: { follow_ups: @follow_ups } %>
<% end %>
So, is calling Stimulus applyFilter, which is working, doing this.element.requestSubmit(), which triggers the controller.
The controller is doing some logic, and responding:
@follow_ups = @follow_ups.limit(20)
respond_to do |format|
format.html
format.turbo_stream { render turbo_stream: turbo_stream.replace("work_queue", partial: "admin/work_queue/work_queue_follow_up_table", locals: { follow_ups: @follow_ups }) }
end
But, something weird happens. If I request with admin_work_queue_path***(:turbo_stream)***, the response on the network tab is correct, the response content-type is: text/vnd.turbo-stream.html, the body is:
…But it doesn’t replace the DOM, and more, the whole page gets replaced by this “text”. If I do it without the (:turbo_stream), the request just fails and returns the whole HTML, instead of just replacing my turbo content. I don’t know what I’m doing wrong.