Hey guys!
First of all. Congratulations. Hotwire is a very good tool for us. I just loved it.
My case:
I want to call multiple api endpoints and based on their responses I want to show the data in the screen without persist. Isn’t relevant persist the information in my db. So I want to create something “read-only” likes. Is possible do something like with Hotwire?
Thank you very much.
Depends. is your Rails server acting only as a API?. Or you mean third-party APIS?.
In case it’s third-party APIS. You can achieve this using streams that are replaced when your action is performed.
Imagine something like this
<div id="api">
</div>
<button>get api results</button>
The button would call an action on the server that will do the API call and render the data, let’s say the request is for a posts_controller.
class PostsController
def api
end
end
app/views/posts/api.turbo_stream.erb
<%= turbo_stream.replace "api" do %>
Your API result here
<% end %>
apart from what @rockwell says, it isn’t clear what you mean? if you want “read only” simply return html? or am i missing something?
Hey Rockwell,
Thanks for the reply.
It’s Third-party apis. I will try do something like you told. Thank you so much for the reply!
1 Like
My bad buddy!
Yes, I just want to render the information (response of few api calls) and just render the information.
Without create the “rule” after_create_commit in their model. Because as I said, I don’t want to persist the information.
you can certainly employ streams without broadcasting…if using POST requests, that’s no problem…but if you want to do with a GET request, since you don’t want to persist anything - consider this comment: Document Turbo Stream Accept: header changes by seanpdoyle · Pull Request #40 · hotwired/turbo-site · GitHub there might be better ways of doing this though. rockwell seems to be employing a similar approach, but with fetch requests within a stimulus controller
another potential way, if you really insist on using streams - i’ve seen in some posts wrapping streams within frames. WARNING: i haven’t actually done it myself, so there could be huge gotchas here:
turbo_frame_tag "api" do
<div id="results" />
<button>get api results</button>
end
# response
turbo_frame_tag "api"
turbo_stream.replace "results" do
<--- results here -->
end
end
…but I would recommend rather than using streams, to simply use a turbo frame. when you press the button, you get the results back in a frame.
1 Like