Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using turbostreams with wicked? #297

Open
APysnack opened this issue Mar 14, 2023 · 1 comment
Open

Using turbostreams with wicked? #297

APysnack opened this issue Mar 14, 2023 · 1 comment

Comments

@APysnack
Copy link

APysnack commented Mar 14, 2023

I'm confused about the pattern someone would use to replace an item in the dom using turbo streams in combination with wicked using the "show action". I'm trying to implement something like this, where In my scenario, I'm trying to replace a nested turboframe "foo" that exists on step_b using the "show" action:

def show
    respond_to do |format|
      case step
      when :step_a
        # dostuff
        
      when :step_b
        format.turbo_stream { render turbo_stream: turbo_stream.update("foo", partial: "partial_path", locals: { bar: params[:baz] }) }
        
      when :step_c
        # dostuff
      
      format.html { render_wizard }
    end
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 making an API call to 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) %> will replace the "foo" turboframe with all the times for 3/14/23.
<%= link_to "Tomorrow", step_b_path(date: 03-15-23) %> will replace the "foo" turboframe with times for 3/15/23.

Ideally, the user can keep clicking these links, which will call the "show" function, which will use the turbostream to 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

However, this doesn't appear to work. In practice, when I implement this, I click "submit" on step_a it

  1. Calls the "update" action (as expected)
  2. Calls the redirect_to_next next_step function (as expected)
  3. Calls the show function for :step_b (as expected)

But the view for step_b never renders. If I look at my network tab, I can see the partial content for the turbo_stream, but the page never renders step_b

@APysnack
Copy link
Author

APysnack commented Mar 16, 2023

Ultimately, I think I was able to work around my specific issue

Clicking this link will change the value of some_variable via turbostreams:

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 } %>

Somewhere inside your 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

I'm not 100% certain, but 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. However, this doesn't seem like an ideal solution

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant