0

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

Adrian-y
  • 1
  • 2
  • Hi @Adrian-y. I run your repo. I don't see this error. – LihnNguyen Mar 19 '23 at 16:29
  • Hi @LihnNguyen, i havent commited the changes yet but only thing different is that i have the above code i mentioned and also the following: ```#show.html.erb <%= turbo_stream_from @post %>``` – Adrian-y Mar 19 '23 at 21:55
  • And also removed the following line from `posts#update action`: ```redirect_and_set_flash_notice(posts_url)``` – Adrian-y Mar 19 '23 at 22:10
  • I tried to fix it but I don't know what you want to do here. I would love to talk to you more. My Linkedin in profile – LihnNguyen Mar 20 '23 at 08:24
  • what i want is to broadcast updates to the index page (i already do), and to the show page. In the show page, upon edit form submission, i want to render the show template. In the index page, upon edit form submission, i want to render the _post partial. – Adrian-y Mar 20 '23 at 09:30

0 Answers0