I have a standard CRUD operation on an Event model that I’m trying to create with Rails/Turbo, but running into issues with the create form. I need to perform two actions after create, so using Streams. One of the actions is to remove the form, which is in its own frame. The other action is to update the list of Event records.
Here’s what I have (I use slim, not ERB):
app/views/events/index.html.slim:
=> link_to 'Add Event', new_admin_event_path, data: { turbo_frame: 'event_form' }
= turbo_frame_tag 'event_form'
= turbo_frame_tag 'events_list' do
== render 'list'
app/controllers/events_controller.rb:
def create
@event = Event.new(event_params)
if @event.save
respond_to do |format|
format.html { redirect_to admin_events_url }
format.turbo_stream
end
end
end
app/views/events/create.turbo_stream.slim:
= turbo_stream.update 'event_form', '' # this is what seems to be the trouble?
= turbo_stream.update 'events_list', Event.all
The issue is that I can’t make the create form work more than once. It’ll create the record, and the turbo_stream file will correctly remove the form, but once that happens the ‘Add Event’ button will no longer work - it won’t populate the event_form
frame again. Or put another way, I can’t add multiple Events without refreshing the page. What am I missing here?