我正在玩 Rails 7 Turbo,我正在嘗試創建一個包含用戶串列下拉串列的表單。該表單可以創建一個新的“單元”記錄并將用戶分配給該單元。該計劃創建一個下拉串列,您可以在其中搜索和選擇單位記錄的所有者。問題是渦輪流回傳一個額外的資料(集合)。我檢查了網路請求,顯然,后端正在發送帶有額外資料的模板。如何在沒有這些額外資料的情況下創建回應并僅回傳部分模板?
額外的資料
[#<User id: 4, email: "foo@bar.com", created_at: "2022-03-18 13:29:04.813660000 0000", updated_at: "2022-03-18 13:29:19.397311000 0000", first_name: nil, last_name: nil, phone: nil, meta: {}, account_id: 2>]
這是完整的回應。
<turbo-stream action="update" target="owner_search_result"><template>
<li
id=user_4
class="hover:bg-blue-600 relative select-none py-2 pl-3 pr-9 text-gray-900 cursor-pointer" id="option-0"
role="option" tabindex="-1">
<span class="block truncate">
</span>
<span >
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">
<path fill-rule="evenodd"
d="M16.707 5.293a1 1 0 010 1.414l-8 8a1 1 0 01-1.414 0l-4-4a1 1 0 011.414-1.414L8 12.586l7.293-7.293a1 1 0 011.414 0z"
clip-rule="evenodd" />
</svg>
</span>
<span hljs-number">2 truncate text-gray-500">
[email protected]
</span>
</li>
[#<User id: 4, email: "[email protected]", created_at: "2022-03-18 13:29:04.813660000 0000", updated_at: "2022-03-18 13:29:19.397311000 0000", first_name: nil, last_name: nil, phone: nil, meta: {}, account_id: 2>]</template></turbo-stream>
這是用戶控制器
def search
keyword = user_params[:keyword] || ""
@users = User.filter_by_email(keyword)
respond_to do |format|
format.turbo_stream do
render turbo_stream: turbo_stream.update(:owner_search_result, partial: 'users/unit_owner', locals: { users: @users })
end
end
end
部分是這個
<%= @users.each do |user| %>
<li
id=<%= dom_id(user) %>
class="hover:bg-blue-600 relative select-none py-2 pl-3 pr-9 text-gray-900 cursor-pointer" id="option-0"
role="option" tabindex="-1">
<span class="block truncate">
<%= user.first_name %>
<%= user.last_name %>
</span>
<span >
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">
<path fill-rule="evenodd"
d="M16.707 5.293a1 1 0 010 1.414l-8 8a1 1 0 01-1.414 0l-4-4a1 1 0 011.414-1.414L8 12.586l7.293-7.293a1 1 0 011.414 0z"
clip-rule="evenodd" />
</svg>
</span>
<span hljs-number">2 truncate text-gray-500">
<%= user.email %>
</span>
</li>
<% end %>
太感謝了。
uj5u.com熱心網友回復:
代替
<%= @users.each do |user| %>
在你的部分開頭
<% @users.each do |user| %>
因為您不想將作為each痛苦用戶實體的呼叫的回傳值呈現到視圖中。注意缺少的=.
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/445704.html
標籤:轨道上的红宝石 红宝石 涡轮链接 ruby-on-rails-7 涡轮
下一篇:來自S3檔案的Zip檔案
