I love stimulus/hotwire but I’m not that good at it!
I am dynamically loading partials based on the occurrence drop down that is select like this:
<select name="scheduleevent[occurrence" id="occurrence" data-controller="remote-select"
data-remote-select-url-value="<%= scheduleevents_select_path %>"
data-remote-select-name-value="form[occurrence]"
data-action="change->remote-select#update" class="mb-4 w-48 bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-primary-500 focus:border-primary-500 block p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-primary-500 dark:focus:border-primary-500">
<%= Scheduleevent.occurrences.each do |occurrence| %>
<option value="<%= new_scheduleevent_path(range: occurrence.second) %>"><%= occurrence.first %></option>
<% end %>
</select>
<%= turbo_frame_tag :ranges do %>
<%= render partial: params[:range] ||= "0", locals: {form: form} %>
<% end %>
Stimulus controller:
import { Controller } from "@hotwired/stimulus"
export default class extends Controller {
static values = {
url: String,
name: String,
}
connect() {
}
update() {
const frame = document.getElementById('ranges');
frame.src = window.event.target.value;
}
}
This works fine to load the partial dynamically based on what value, 0, 1, etc. is passed. The only issue is that when I submit the controller the ocurrence value isn’t being saved which I need it to be. It is trying to write the URL /scheduleevents/new?range=1 into an integer field for occurrence. Is there a better way to rewrite this so I am not passing the URL through the select dropdown value so I can retain the selected value?