ruby-on-rails 使用Turbo Stream实现Rails的多个部分更新

5vf7fwbs  于 2023-02-17  发布在  Ruby
关注(0)|答案(1)|浏览(287)
    • bounty将在5天后过期**。此问题的答案可获得+200声誉奖励。doer123456789正在寻找来自声誉良好来源的答案:该解决方案需要使用两个不同的分瓣,并且需要正确的模型、控制器和视图,包括Turbo和Stimulus解决方案。

我有两个模型,一个用于客户,一个用于投诉,一个客户也有很多投诉。如果向客户添加了投诉,如何使用Turbo Stream更新投诉索引和客户的显示视图(使用两个不同的部分),同时确保客户显示视图中仅列出特定客户的投诉?
下面是我的一些代码:
客户型号:

class Customer < ApplicationRecord
    validates_presence_of :names
    belongs_to :company
    has_many :complaints

    broadcasts_to ->(customer) { [customer.company, "customers"] }, inserts_by: :prepend
end

投诉型号:

class Complaint < ApplicationRecord
    belongs_to :company
    belongs_to :customer

    broadcasts_to ->(complaint) { [transgression.company, "complaints"] }, inserts_by: :prepend
end

投诉索引. html. erb(流媒体罚款):

<%= turbo_stream_from @company, "complaints" %>

<h1>Complaints</h1>

<%= link_to new_complaint_path, class: "btn", data: { turbo_frame: "remote_modal" } do %>
  New
<% end %>
<div class="table-responsive">
  <table class="table">
    <thead>
      <tr>
        <th scope="col">Customer</th>
        <th scope="col">Date</th>
        <th scope="col">Note</th>
        <th colspan="2"></th>
      </tr>
    </thead>
    <tbody id="complaints">
      <%= render @complaints %>
    </tbody>
  </table>

客户显示. html. erb视图(非流...):

<%= turbo_stream_from @customer %>
<%= turbo_stream_from @customer, "complaints" %>

  <%= turbo_frame_tag "customer" do %>
    <%= @customer.names %>
  <% end %>

  <%= link_to edit_employee_path(@customer), :class => "btn", data: { turbo_frame: "remote_modal" } do %>
    Edit
  <% end %>

  <h4>Complaints</h4>

  <%= link_to new_complaint_path, class: "btn", data: { turbo_frame: "remote_modal" } do %>
    Add
  <% end %>

  <div class="table-responsive">
    <table class="table">
      <thead>
        <tr>
          <th scope="col">Date</th>
          <th scope="col">Note</th>
          <th colspan="2"></th>
        </tr>
      </thead>
      <tbody id="complaints">
        <%= render @complaints %>
      </tbody>
    </table>
  </div>
bwitn5fc

bwitn5fc1#

在客户展示页面上,您订阅了@customer, "complaints",但您没有向它广播任何内容,因此没有更新。

# app/models/complaint.rb

class Complaint < ApplicationRecord
    belongs_to :company
    belongs_to :customer

    # NOTE: this will send turbo streams to `[company, "complaints"]`
    #       you have to be subscribed to that channel to receive updates.
    # Defaults:
    #       partial: "complaints/complaint"
    #       target:  "complaints"
    broadcasts_to ->(complaint) { [complaint.company, "complaints"] }, inserts_by: :prepend

    # NOTE: to receive updates on the customer show page you have send
    #       updates to [customer, "complaints"]
    broadcasts_to ->(complaint) { [complaint.customer, "complaints"] },
      inserts_by: :prepend,
      partial: "customers/complaint" # <= if you want to use a different partial
end
# app/views/customers/show.html.erb

# make sure to subscribe to a channel that you're broadcasting to
<%= turbo_stream_from @customer, "complaints" %>

# you have to have an `id` target for the incoming `turbo_stream`
<div id="complaints">
  # render a different partial
  <%= render partial: "customers/complaint", collection: @complaints %>
</div>

我不知道你的控制器有什么问题,你不需要做任何广播工作:

# app/controllers/customers_controller.rb

# GET /customers/1
def show
  # NOTE: only show specific customer complaints on initial request
  #       broadcasts are already limited to `@customer`
  @complaints = @customer.complaints
end

相关问题