Search code examples
ruby-on-railsruby-on-rails-7hotwire-railsturborails7

RoR Rails 7 using Turbo to Update a partial on button click


Help! I'm relatively new to Ruby on Rails and now I'm attempting to use Rails 7 with Turbo and Stimulus to update a partial on my web page.

I have buttons on a page within a partial (call it _home.html.erb). When clicked I'd like them to replace _home.html.erb with a different partial.

Let me show you what I've done so far:

view:

index.html.erb
<%= turbo_frame_tag "main-speaker-div" do %>
    <%= render partial: 'home' %>
  <% end %>```

_home.html.erb
<div class="row">
  <div class="col-5">
        
        <%= button_to "Settings", rendersettings_path(), remote: true, class: "reader-general-button"%>

  </div>
</div>

controller:
  def rendersettings
    broadcast_replace_to "main-speaker-div", partial: 'settings'
  end

This code is throwing the following error: POST 500 http://localhost:3000/rendersettings (Internal Server Error) Response has no matching element

Server console error: NoMethodError (undefined method `broadcast_replace_to' for #HreaderController:0x0000000002be30):

Thanks in advance for your help!


Solution

  • I actually ended up doing the following to with my view to fix this issue:

    <%= turbo_stream_from "main-speaker-div" %>
     <%= turbo_frame_tag "main-speaker-div" do %>
        <turbo-frame id="main-speaker-div">
        <%= render partial: 'homefull' %>
        </turbo-frame>
      <% end %>
    

    It seems like if I am broadcasting I would need to have the turbo_stream_from tag added as a listener. Please point out any issues you see with this! I'm basically doing trial and error since this is so new to RoR