im building a facebook clone app with rails and im using Turbo Streams to broadcast live changes to users subscribed to the streams. My problem is that when a user is on the show page
of a post and they edit
the post and submit
the changes, the _post.html.erb
partial is rendered instead of the show
view being rendered. Currently im only broadcasting
the updates using the _post.html.erb
partial like the following:
# post.rb
after_update_commit -> do
broadcast_update_later_to [self.user.id, "posts"], partial: "posts/post", locals: { post: self, user: Current.user }, target: "posts"
end
# update.turbo_stream.erb
<%= turbo_stream.update(@post, partial: "posts/post", locals: { post: @post, user: Current.user } ) %>
However, what i want to do is to also broadcast updates to the show
page and instead of rendering the _post.html.erb
, i render the show template via template: "posts/show"
.
i tried adding another broadcast call inside the after_update
callback like this:
# post.rb
after_update_commit -> do
broadcast_update_later_to [self.user.id, "posts"], partial: "posts/post", locals: { post: self, user: Current.user }, target: "posts"
broadcast_update_later_to self, template: "posts/show", target: self
end
# update.turbo_stream.erb
<%= turbo_stream.update(@post, partial: "posts/post", locals: { post: @post, user: Current.user } ) %>
<%= turbo_stream.update(@post, template: "posts/show", target: @post ) %>
However that did not work as i was getting the following error in my console:
Error performing Turbo::Streams::ActionBroadcastJob from Async(default) ActionView::Template::Error (undefined method `user' for nil:NilClass <h5 class="card-title">'.freeze;@output_buffer.append=( @post.user.username );@output_buffer.safe_append='</h5> ^^^^^):
Not sure why i am getting this error as the @post variable is clearly defined in my controller so idk what's causing it.
Github Repository: https://github.com/adrian-y1/odin-facebook