1

I'm trying to replace the value of the dropdown though the turbo stream.

through js request we can do as

views/projects/filter_units_by_organization.js.erb

$('select#project_unit_id').html('<%= j options_from_collection_for_select(@filtered_units, :id, :name) %>');

for turbo the file is as

views/projects/filter_units_by_organization.turbo_stream.erb

<%= turbo_stream.replace "option" do %>
 
<% end %>

views/projects/_form.html.erb

<div class="field">
    <%= f.label :name %><br>
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :description %><br>
    <%= f.text_area :description %>
  </div>
  <div class="field">
    <%= f.label :organization_id %><br>
    <%= f.collection_select :organization_id, Organization.all, :id, :name, { prompt: 'Please select' } %>
  </div>
  <div class="field">
    <%= f.label :unit_id %><br>
    <%= f.collection_select :unit_id, Unit.all.where(organization_id: :organization_id), :id, :name %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>

I am trying to use a similar approach in the case of turbo stream. Turbo is replacing the complete div, is it possible to replace the select tag only?

######### Solution #########

<%= turbo_stream.update "project_unit_id" do 
  options_from_collection_for_select(@objects, :id, :name) %>
<% end %>

if you want to add include blank value then use

<%= turbo_stream.update 'project_unit_id' do %>
  <%= options_for_select(@objects.collect{ |obj| [obj.name, obj.id] }.unshift(['Select Value', nil])) %>
<% end %>

Akash Kinwad
  • 704
  • 2
  • 7
  • 22

0 Answers0