Skip to content

Turbo-streams and turbo-frames with responders #230

@brendon

Description

@brendon

I've come up against an interesting problem. This page has an interesting technique for creating a modal with turbo: https://www.viget.com/articles/fancy-form-modals-with-rails-turbo/

I'm trying to specifically reproduce this logic with responders:

def update
  if current_user.update(user_params)
    respond_to do |format|
      format.turbo_stream do
        render turbo_stream: [
          turbo_stream.update("flash", partial: "shared/flash", locals: { notice: "Profile updated" }),
          turbo_stream.update("user-about", partial: "users/about", locals: { user: current_user })
        ]
      end

      format.html do
        redirect_to current_user, notice: "Profile updated"
      end
    end
  else
    render :edit, status: :unprocessable_entity
  end
end

So to translate, the initial edit form is just a html view wrapped in a turbo-frame tag called 'modal'. Navigating to this updates the modal with the form. We then use stimulus to show the modal.

When submitting the form: if the model saves we want to render a turbo-stream response that updates various areas of the page. If it fails we want to render the original html edit page from before with the form field errors highlighted etc...

So far I can't seem to figure out how to make this work with responders.

Does anyone have any tips? :)

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions