I have a basic Stimulus controller that does debounce and request submit for a input field (search behavior) and when I type in the request goes as HTML, with an index action, it returns well and replace everything (it responds true to turbo_frame_request?) but when I use the exact same thing in another website the request goes as JS and the request responds true to request.xhr? (using a show action) and of course it is not captured by the turbo_frame and it does not replace the content.
My question is why does it work for one and not for the other? I don’t have any turbo_frame surrounding the input field, just a form as the working page.
In Rails 6.0 and 5.2, all forms using `form_with` implement `remote: true` by default. These forms will
submit data using an XHR (Ajax) request. To disable this include `local: true` . To dive deeper see
[Working with JavaScript in Rails]
(https://guides.rubyonrails.org/working_with_javascript_in_rails.html#remote-elements) guide.
I’m using now:
<%= form_with url: '', local: true, data: { ...
and now it is refreshing the entire page, what I need now is to have the specific turbo frame replaced, I mean the request is not a turbo_frame request yet.
@tleish I could receive the request as TURBO_STREAM by add this data: { turbo_stream: '', ... to the form and then modifying the Stimulus search_controller like this:
At quick glance, it’s not completely clear to me the logic of the stimulus controller or why you need to use a stimulus controller to submit a form to a turbo-frame, but when submitting a page through a turbo-frame, turbo adds the header Turbo-Frame: entities-lists. I see from the headers this is missing.
# frozen_string_literal: true
module App
class SearchesController < ::App::BaseController
def index
@search = { query: search_params[:search] }
end
def search_params
params.permit(:search)
end
end
end
SearchesController index view file:
<%= turbo_stream.update('searches-frame') do %>
<%= "Rafa #{@search[:query]}" %>
<% end %>
The issue now is that the index view is not rendering anything back on the request.
I’d suggest to go back to basics and remove the entire JS, see what happens then. Remove local: true/false from the form as well.
I don’t think in this case you’d need Turbo Stream, or custom headers / custom request. Rremove turbo_stream: '' this.
This should be a turbo frame turbo_stream.update('searches-frame') do as @tleish suggested.
I’d also suggest removing the turbo frame and seeing if the request is HTML then. If it’s not, in Firefox, you can see what JS events are attached to the form element in the inspector, and spot any other JS that might be coming into play.