0

I'm trying to create a form with a turbo_stream format request. This is my form

<%= form_with url: search_stock_path(format: :turbo_stream), data:{ method: :get, turbo: true}, method: :get, turbo: true do |f| %>
            <div class="form-group row">
                <div class="col-sm-9 no-r-padding">
                    <%= text_field_tag :stock, params[:stock], placeholder:"Stock ticker symbol", autofocus: true, class:"form-control form-control-lg", required: true%>
                </div>
                <div class="col-sm-3 no-l-padding">
                    <%= button_tag class: "btn btn-success" do %>    
                        <%=  fa_icon 'search 2x' %>
                    <% end %>
                </div>
            </div>
        <% end %>

<%= content_tag :div, id: dom_id(@stock) do %>
heloooooo
<% end %>

My controller

def search_stock
    if params[:stock].present?
      @stock = Stock.new_lookup params[:stock]
      if @stock
        respond_to do |format|
          format.turbo_stream { render turbo_stream: turbo_stream.remove(dom_id(@stock))}
        end
      else
        flash[:alert] = 'Please enter a valid symbol'
        redirect_to my_portfolio_path
      end
    else
      flash[:alert] = 'Please enter a symbol'
      redirect_to my_portfolio_path
    end
  end

When I submit the action I get as response a redirection with this code instead of removing the


<%= content_tag :div, id: dom_id(@stock) do %>
heloooooo
<% end %>

enter image description here

abner DC
  • 11
  • 2
  • Did you ever get an answer to this? Having the same issue. I think its related to setting the URL? – cal1801 Feb 02 '23 at 15:40

1 Answers1

0

You can't attach the turbo stream format to a url and use as your example.

In older versions of turbo, you can't do this at all with a form, see:

https://github.com/hotwired/turbo/pull/612

Apparently this is now available if you upgrade to turbo-rails 1.3 and use data: { turbo_stream: true }

Note, you might also need to update your package.json to use turbo-rails 7.3.

riley
  • 2,387
  • 1
  • 25
  • 31