<%= post.user.firs" />
<%= post.user.firs" />
<%= post.user.firs"/>
DEVHIDE
  • Home (current)
  • About
  • Contact
  • Cookie
  • Home (current)
  • About
  • Contact
  • Cookie
  • Disclaimer
  • Privacy
  • TOS
Login Or Sign up

turbo_stream first comment is getting replace by the other comment without reloading page

24 Views Asked by Yash At 12 March 2024 at 11:53 2025-12-24T04:21:21.120000

_post.html.erb

  <div class="card text-center bg-secondary my-2" style="width: 18rem;">
                <div class="card-footer text-body-secondary">
                    <%= post.user.first_name %> <%= post.user.last_name%> posted <%= time_ago_in_words(post.created_at) %> ago
                </div>
                <div class="row">
                    <% if post.medias.attached? %>
                        <% post.medias.each do |media| %>
                            <div class="col-md-6">
                                <%= image_tag file_path(media), class: "rounded mx-auto d-block w-100 mt-2" %>
                            </div>
                        <% end %>
                    <% end %>
                </div>
                <div class="card-body">
                    <p class="card-text"><%= post.description %></p>
                    <%= link_to "View Post", post_path(post), class: "btn btn-dark" %>
                </div>
                <div class="mb-2 row">
                    <div class="col-sm-4">
                        <div class="row">
                            <div class="text-center my-2">
                                <%= pluralize(post.likes.count, "like") %>
                            </div>
                            <div class="text-center my-2"> 
                                <% if current_user_post_like?(post) %>
                                    
                                    <%= link_to likes_path({id: post}), data: {turbo_stream: true, turbo_method: :delete} do %>
                                        <i class="fa-solid fa-heart fa-2xl" style="color: #e31621;"></i>
                                    <% end %>
                                <% else %>
                                    <%= link_to likes_path({id: post}), data: {turbo_stream: true, turbo_method: :post} do %>
                                        <i class="fa-regular fa-heart fa-2xl" style="color: #e1dada;"></i>
                                    <% end %>
                                <% end %>
                            </div>

                        </div>
                    </div>
                    <div class=" col-sm-4 flex-grow-1 p-0">
                        <div class="row">
                            <div class="my-2">
                                <%= pluralize(post.comments.count, "comment") %>
                            </div>
                            <div class="my-2">
                                <%= render 'shared/comments', {post: post} %> 
                            </div>
                        </div>
                    </div>
                </div>
            </div>

_comment.html.erb

<div class="d-flex flex-start">
    <% if comment.user.photo.attached? %>
        <%= image_tag file_path(comment.user.photo) , alt: "avatar", width: "65", height: "65", class: "rounded-circle shadow-1-strong me-3" %>
    <% else %>
        <svg xmlns="http://www.w3.org/2000/svg" width="65" height="65" fill="currentColor" class="bi bi-person rounded-circle shadow-1-strong me-3" viewBox="0 0 16 16">
            <path d="M8 8a3 3 0 1 0 0-6 3 3 0 0 0 0 6m2-3a2 2 0 1 1-4 0 2 2 0 0 1 4 0m4 8c0 1-1 1-1 1H3s-1 0-1-1 1-4 6-4 6 3 6 4m-1-.004c-.001-.246-.154-.986-.832-1.664C11.516 10.68 10.289 10 8 10s-3.516.68-4.168 1.332c-.678.678-.83 1.418-.832 1.664z"/>
        </svg>
    <% end %>
    <div class="flex-grow-1 flex-shrink-1">
        <div>
            <div class="d-flex justify-content-between align-items-center">
                <p class="mb-1">
                    <%= comment.user.first_name %> <span class="small">- <%= time_ago_in_words(comment.created_at) %> ago</span>
                </p>
            </div>
            <p class="small text-start mb-0">
                <%= comment.content %>
            </p>
        </div>
    </div>
</div>

comments_controller.rb

  def create
    @comment = current_user.comments.build(comments_params)
    @comment.post = @post
    if @comment.save
      respond_to do |format|
        format.turbo_stream {  }
      end
    end
  end

create.turbo_stream.erb

<turbo-stream action="append" target="messages">
  <template>
    <div id="message_1">
        <div class="d-flex flex-start">
    <% if @comment.user.photo.attached? %>
        <%= image_tag file_path(@comment.user.photo) , alt: "avatar", width: "65", height: "65", class: "rounded-circle shadow-1-strong me-3" %>
    <% else %>
        <svg xmlns="http://www.w3.org/2000/svg" width="65" height="65" fill="currentColor" class="bi bi-person rounded-circle shadow-1-strong me-3" viewBox="0 0 16 16">
            <path d="M8 8a3 3 0 1 0 0-6 3 3 0 0 0 0 6m2-3a2 2 0 1 1-4 0 2 2 0 0 1 4 0m4 8c0 1-1 1-1 1H3s-1 0-1-1 1-4 6-4 6 3 6 4m-1-.004c-.001-.246-.154-.986-.832-1.664C11.516 10.68 10.289 10 8 10s-3.516.68-4.168 1.332c-.678.678-.83 1.418-.832 1.664z"/>
        </svg>
    <% end %>
    <div class="flex-grow-1 flex-shrink-1">
        <div>
            <div class="d-flex justify-content-between align-items-center">
                <p class="mb-1">
                    <%= @comment.user.first_name %> <span class="small">- <%= time_ago_in_words(@comment.created_at) %> ago</span>
                </p>
            </div>
            <p class="small text-start mb-0">
                <%= @comment.content %>
            </p>
        </div>
    </div>
</div>

    </div>
  </template>
</turbo-stream>

When I add a comment without reloading the page, it appears instantly, but subsequent comments replace the previous one until the page is refreshed, displaying all comments. I expect that when I add a second comment without reloading, both comments should remain visible without replacing each other.

ruby-on-rails ruby-on-rails-7 hotwire-rails turbo
Original Q&A
1

There are 1 best solutions below

0
Alex Alex On 12 March 2024 at 15:31
<!-- create.turbo_stream.erb -->

<turbo-stream action="append" target="messages">
  <template>
    <div id="message_1"> <!-- because you have this hardcoded id -->
      ...
    </div>
  </template>
</turbo-stream>

If the template’s first element has an id that is already used by a direct child inside the container targeted by dom_id, it is replaced instead of appended.

https://turbo.hotwired.dev/reference/streams#append

Change it to something like this:

<%= turbo_stream.append :messages do %>
  <div id="<%= dom_id(@comment) %>">
    ...
  </div>
<% end %>

Related Questions in RUBY-ON-RAILS

  • How to display legend box in tooltip text for amCharts 5 in Rails application?
  • how to integrate cashfree payment gateway in ruby on rails project
  • RSpec Capybara throwing Selenium error when trying to click a button with browser confirm
  • rails minitest not picking up fixture properly, instance variable not percolating
  • Duplicate GET requests - Rails & Heroku
  • How to stub out current_user in JWT model for Rspec?
  • NameError in Home#index
  • Verifying Google Identity OAuth2 token with Ruby
  • Error WebMock::NetConnectNotAllowedError in testing with stub using minitest in rails (using Faraday)
  • why is mission_control-jobs erroring with load path error?
  • Rescuing validation errors from a polymorphic association
  • New error on random number assigned to local variable , Rails
  • How to fix error in model with gem lockbox
  • Images uploaded via Active Storage not displaying in Active Admin or on certain devices
  • controller test_methods generating two errors intermittently

Related Questions in RUBY-ON-RAILS-7

  • What does instantiating an ActiveModel::Model class within itself provide from a design perspective?
  • Rails 7.1.1 - spawning error occurred - passenger - You have already activated stringio 3.0.0, but your Gemfile requires stringio 3.0.8
  • How to use JavaScript in Rails 7?
  • Set-Cookie header in rails 7 wrongly set as array
  • Rendering template dynamically in Rails ViewComponent
  • Next.JS 13.4 Cookies() Function Works In Route Handler In Development But Not Production
  • How can I override PostgreSQL's gen_random_uuid() in Rails?
  • Rails javascript delegation for tinymce-rails in a nested fields partial
  • Is there a way to unsubscribe from Turbo StreamChannels after Devise logged out a user of inactivity?
  • jsonapi-serializer - Return slug instead of id for relationship
  • Activeadmin Rails 7 Tailwind Issue
  • Trouble passing a data structure from Rails 7 to a javascript function
  • Rails 7 with turbo and JS event listeners
  • HMR not working when updating app/components/{component_name}.html.erb in Rails 7
  • How to configure Rails to use subfolders in models with RSpec?

Related Questions in HOTWIRE-RAILS

  • Setting button/other element display state based on form input values using Hotwire Stimulus
  • Issues with Turbo Stream updates in Rails real-time voting app with iterative "rounds" after second iteration
  • turbo_stream first comment is getting replace by the other comment without reloading page
  • Nested partials were not working in turbo stream rails 7
  • Partial render with turbo stream is not working in rails 7
  • Connection issues with turbo streams and custom action cable channel
  • Rails with Hotwire/Turbo doing get requests on link hover
  • How do I populate a second dropdown menu based on the selection of the first dropdown menu with Rails/Hotwire/Turbo/Stimulus?
  • Rails turbo "data-turbo-action: advance", handling refresh, src, and maintaining query params in turbo frames
  • Rails / Turbo Stream prepend outside a div so the CSS is not implemented on the inserted element
  • why does turbostream just render only the turboframe
  • turbo-progress-bar in Rails 7 does not hide after complete
  • Chartkick + Rails 7 (Turbo) + Heroku
  • Rails 7 new app with --css bootstrap - turbo buttons won't work
  • Rails 7 + Turbo: Do not show loading state when navigating

Related Questions in TURBO

  • Rails turbo content missing after turbo response
  • Issues with Turbo Stream updates in Rails real-time voting app with iterative "rounds" after second iteration
  • Symfony Turbo download the html file instead of rendering it
  • turbo_stream first comment is getting replace by the other comment without reloading page
  • Nested partials were not working in turbo stream rails 7
  • Partial render with turbo stream is not working in rails 7
  • Issue with current_user in turbo_stream and broadcast
  • Unexpected login behaviour in production in my Rails 7 app
  • Rails with Hotwire/Turbo doing get requests on link hover
  • Correctly updating turbo frame source via stimulus controller
  • Starting issue using expo of a react native sample from turbo
  • Rails turbo stream only updating one element on the page
  • How can I get the "signed-stream-name" from a turbo:before-stream-render event?
  • Rails turbostream: is it possible to add a css class dynamically to an element?
  • turbo-progress-bar in Rails 7 does not hide after complete

Trending Questions

  • UIImageView Frame Doesn't Reflect Constraints
  • Is it possible to use adb commands to click on a view by finding its ID?
  • How to create a new web character symbol recognizable by html/javascript?
  • Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
  • Heap Gives Page Fault
  • Connect ffmpeg to Visual Studio 2008
  • Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
  • How to avoid default initialization of objects in std::vector?
  • second argument of the command line arguments in a format other than char** argv or char* argv[]
  • How to improve efficiency of algorithm which generates next lexicographic permutation?
  • Navigating to the another actvity app getting crash in android
  • How to read the particular message format in android and store in sqlite database?
  • Resetting inventory status after order is cancelled
  • Efficiently compute powers of X in SSE/AVX
  • Insert into an external database using ajax and php : POST 500 (Internal Server Error)

Popular # Hahtags

javascript python java c# php android html jquery c++ css ios sql mysql r reactjs

Popular Questions

  • How do I undo the most recent local commits in Git?
  • How can I remove a specific item from an array in JavaScript?
  • How do I delete a Git branch locally and remotely?
  • Find all files containing a specific text (string) on Linux?
  • How do I revert a Git repository to a previous commit?
  • How do I create an HTML button that acts like a link?
  • How do I check out a remote Git branch?
  • How do I force "git pull" to overwrite local files?
  • How do I list all files of a directory?
  • How to check whether a string contains a substring in JavaScript?
  • How do I redirect to another webpage?
  • How can I iterate over rows in a Pandas DataFrame?
  • How do I convert a String to an int in Java?
  • Does Python have a string 'contains' substring method?
  • How do I check if a string contains a specific word?
.

Copyright © 2021 Jogjafile Inc.

  • Disclaimer
  • Privacy
  • TOS
  • Homegardensmart
  • Math
  • Aftereffectstemplates