The 'helpers' proxy isn't available in turbo_stream helpers #486
-
at least if I did everything correctly..... <% @staged.each do |item| %> Obviously, I we're able to include the required helper, but it would be nice if this fits into Phlex automagically. Also, any cleaner way to handle that loop? Thanks for a really great project. |
Beta Was this translation helpful? Give feedback.
Replies: 8 comments 1 reply
-
I could be mistaken here, but I believe that because you are handling the rendering yourself in this code (you're the one calling <% @staged.each do |item| %>
<%= turbo_stream.append 'staged', Views::StagedItem.new(item:).call(view_context: self) %>
<% end %> Without knowing where this code is being called from, I'm not sure if |
Beta Was this translation helpful? Give feedback.
-
@willcosgrove Thank you. That resolved the issue. To answer your question, this is a stock turbo_stream response template (i.e., index.turbo_stream.erb) |
Beta Was this translation helpful? Give feedback.
-
We should make it easier to render a Phlex view for a turbo stream while having it take on the view context implicitly. |
Beta Was this translation helpful? Give feedback.
-
@hms I was just looking at the code in turbo-rails to see why it didn't support rendering any renderable, and it looks like they have an optional But I also noticed you can pass a block and then render a Phlex view in that block. # app/controllers/messages_controller.rb
class MessagesController < ApplicationController
def index
render Views::Messages::Index.new(
messages: Message.all
)
end
def create
message = Message.create!(params.require(:message).permit(:content))
respond_to do |format|
format.turbo_stream do
render turbo_stream: turbo_stream.append(:messages) do
render Components::Messages::Message.new(message:)
end
end
format.html { redirect_to messages_url }
end
end
end I think this interface is pretty reasonable for now and maybe there are improvements that can be made in the future to have it accept a renderable object as that second positional argument. |
Beta Was this translation helpful? Give feedback.
-
I’m going to convert this issue to a discussion as I think it will be helpful for others in the future. |
Beta Was this translation helpful? Give feedback.
-
Thanks for looking into this and the follow up. I'm slowly converting all of my partials to Phlex and finding the process mostly painless and love the end result. The code in question above was in an .erb file (started life as a bunch of partials). In the .erb context, I was unable to figure out how to avoid requiring .call(view_context: self). hms |
Beta Was this translation helpful? Give feedback.
-
Yes, although I had to dredge up from a long ago memory and a little goggling... For the life of me, I couldn't remember the compound form for render turbo_stream. respond_to do |format|
render turbo_stream: [
turbo_stream.append('target', partial: .... ),
# NOTE: .call is not optional
turbo_stream.append('phlex-component', Views::Component.new().call(view_context: self)
]
end I believe Phlex-rails 0.6 should address that pesky call(view_context: self), but I've been struggling to put together the correct includes. |
Beta Was this translation helpful? Give feedback.
-
The solution is to use turbo_stream.append "notifications", NotificationComponent.new(@notification) |
Beta Was this translation helpful? Give feedback.
The solution is to use
turbo-rails
version >= 1.4 like this.