-
Notifications
You must be signed in to change notification settings - Fork 156
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
Turbo-streams and turbo-frames with responders #230
Comments
I managed to get there in the end. Is this the most efficient way to write it? def update
respond_with online_newsletter,
location: [:admin, online_newsletter] do |format|
if online_newsletter.update(online_newsletter_params)
format.turbo_stream {
render turbo_stream: turbo_stream.update("header", partial: "header")
}
else
format.html { render 'edit', status: :unprocessable_entity }
end
end
end |
I'm using this guy's responder but I think it needs to be modified to play nice with the flash responder. He's only trying to deal with Devise issues whereas I'm using Responders generally (because why on Earth would people want to manually do this in all their controller actions?). https://www.driftingruby.com/episodes/hotwire-turbo-replacing-rails-ujs So far I'm getting some mileage out of this: class ApplicationResponder < ActionController::Responder
module TurboFlashResponder
include(Responders::FlashResponder)
def to_turbo_stream
to_html
end
end
include(TurboResponder)
include(TurboFlashResponder)
include(Responders::HttpCacheResponder)
end
# https://gorails.com/episodes/devise-hotwire-turbo
module TurboResponder
def navigation_behavior(error)
if get?
raise error
elsif has_errors? && default_action
render(rendering_options.merge(formats: [:turbo_stream, :html], status: :unprocessable_entity))
else
redirect_to navigation_location
end
end
def options
super.merge(formats: [:turbo_stream, :html])
end
end |
Thanks @ClayShentrup, that looks like a great start. @carlosantoniodasilva, do you think there is appetite to make turbo responses a first class citizen in |
In case it's helpful for anyone else, this is what's working for me to make Turbo work with Responders. Inside application_responder.rb: def initialize(controller, resources, options = {})
super
if [:js, :turbo_stream].include?(format)
options[:formats] ||= request.formats.map(&:symbol)
end
end
alias :to_turbo_stream :to_html |
@rafaelfranca what's the resolution here? is the plan not to offer a |
Not sure if this should have been closed. In my excursion into broadcast turbo streams I'm finding that I want to render
I'm not a fan of broadcasting from models. I also struggled with the built-in rendering functionality with
|
I am going to reopen this to remind me to look into |
Thanks @carlosantoniodasilva, let me know if you need to test anything in the wild :) For now in my
Including |
Reduced to : # frozen_string_literal: true
class ApplicationResponder < ActionController::Responder
include Responders::FlashResponder
include Responders::HttpCacheResponder
self.error_status = :unprocessable_entity
self.redirect_status = :see_other
alias :to_turbo_stream :to_html
end |
@n-rodriguez that is great :) Can you clarify what's your use for the I haven't circled back yet on that from responder's perspective, but I'm guessing I might have to add something along those lines to responders itself so it can handle those appropriately. |
@carlosantoniodasilva, I think But for me it was not enough. Flash messages have to be displayed immediately and not on the next request. Thus i had to change the
|
Without
|
I also needed When a request with a turbo stream format comes in:
it would respond with this:
Now with this setup it will correctly render and return the
|
I find myself working on this again :D I think I've come closer to something that works. My scenario is this:
In my use case I render When submitted, if the model is valid, I want to render I've come up with the following as modifications to
There will be other ways to skin the cat but the main problem with Responders is that it relies on rescuing In my changes:
So far this seems to works. I'm sure there will be quirks as I go along, and perhaps this is particular to my use case, but I think Turbo Streams aren't going away and it might be time to bake something in, or at least have an extra responder in Responders to optionally include to handle turbo_streams more explicitly? |
Thinking slightly harder, it could be that we could extend the configuration to allow for defining formats for the different request types (get, error, success) to keep everything agnostic? |
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:
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? :)
The text was updated successfully, but these errors were encountered: