I'm confused about the pattern someone would use to replace an item in the dom using turbo streams in combination with wicked wizard using the "show action". In my scenario, I'm trying to replace a nested turboframe "foo" that exists on step_b using the "show" action, which looks like this:
def show
case step
when :step_a
when :step_b
when :step_c
render_wizard
end
In step_b's view, I have several links, and they all link back to step b, but with different params. (I'm actually using an API call to fetch and render times in an appointment picker, and the params represent the dates for those times).
<%= link_to "Today", step_b_path(date: 03-14-23) %>
Would ideally replace the "foo" turboframe inside step_b's view with all the times for 3/14/23.
<%= link_to "Tomorrow", step_b_path(date: 03-15-23) %>
Would ideally replace the "foo" turboframe inside step_b's view with all the times for 3/15/23.
The user can keep clicking these links, which will update the appointment times, until they finally pick and submit their appointment selection for that step in the form. Once they select a time and click "submit" it calls the update
action and proceeds to step_c
I've seen that if I have a <%= turbo_frame_tag "foo" %>
on step_a, I can create a step_b.turbo_stream.erb
to replace the turboframe on step a by doing something like this:
<%= turbo_stream.replace "foo", partial: "partial_path", locals: { bar: @baz } %>
However, I'm having issues with this because it seems like this only gets called when a user "submits" and calls the update function. What I want is for the turboframe to update when they click on a link to step_b, which only calls the "show" action. The nested turbo frame I'm trying to update only exists on step_b.
However, when I try to add something like this to the show
action:
when :step_b
respond_to do |format|
format.turbo_stream { render turbo_stream: turbo_stream.replace("foo", partial: "partial_path", locals: {bar: params[:baz]
})}
end
it causes a double render conflict with render_wizard
. I've tried doing an if/else statement like this:
if step == :times
respond_to do |format|
format.turbo_stream { render turbo_stream: turbo_stream.replace("foo", partial: "partial_path", locals: {bar: params[:baz] } )}
format.html { times_path }
end
else
render_wizard
end
Which doesn't render the step at all. I haven't been able to find much information that connects wicked and turbostreams. I have many more steps other than just step_b
that also use the render_wizard
method, and was wondering if there's a way to accomplish this without a massive refactor?
per the docs I've seen (linked below), it states "You'll need to call render_wizard at the end of your action to get the correct views to show up." and if the turbo_stream view conflicts with render_wizard, it sounds like this might not be possible.
https://www.rubydoc.info/gems/wicked/1.3.4
all the examples I've seen using turbostream just tend to assume you have a very typical rails scaffold setup, where the "create" action maps to the create.turbo_stream.erb
and the "delete" action maps to the delete.turbo_stream.erb
, so it's hard to figure out exactly how to implement the same solutions if the controller looks different than those, as they do when using wicked wizard
EDIT:
See the comment below from Alex who was able to provide me with an answer for my issue. I just wanted to post a quick summary:
Somewhere in my step_b view, I have something similar to this -
step_b.html.erb:
<%= turbo_frame_tag "foo_frame" do %>
<%= some_variable %>
<%= end %>
<%= link_to "step b", step_b_path(some_variable: "new value of some_variable"), data: { "turbo_stream": true } %>
As seen above, in the view for step_b you'll need a link with data-turbo-stream=true
. This will cause the link click to be of type "text/vnd.turbo-stream.html", which is a typical turbo-stream request. Then when we later do respond_to do |format|
that will tell rails to execute the format.turbo_stream
code in your steps controller.
steps_controller.rb
def show
respond_to do |format|
case step
when :step_a
# dostuff
when :step_b
if request.referrer.include? wizard_url
format.turbo_stream { render turbo_stream: turbo_stream.update("foo_frame", partial: path_to_partial, locals: { some_variable: params[:some_variable] }) }
end
when :step_c
# dostuff
format.html { render_wizard }
end
end
For the issue I was running into, it seems that when navigating from step_a to step_b it's trying to process it as a turbostream and doesn't render the view for step_b. It may be because I have a nested turbostream in my case. However, I wanted to be able to target and replace a specific, nested turbostream.
The line if request.referrer.include? wizard_url
basically just says "If the request is coming from the same step_b page, then perform this turbostream update on the "foo" turboframe. Therefore this code won't execute when transitioning from step_a to step_b, which resolves the issue described above
Thanks to Alex for all of his help, and providing a solution