轨道 7 + Turbo:turbo_stream 在部分模板上响应额外数据?
我正在玩 Rails 7 + Turbo,我正在尝试创建一个包含用户列表下拉列表的表单。该表单可以创建一个新的“单元”记录并将用户分配到该单元。该计划创建一个下拉列表,您可以在其中搜索并选择单位记录的所有者。问题是 Turbo 流返回额外的数据(集合)。我检查了网络请求,显然后端正在发送带有额外数据的模板。如何在没有这些额外数据的情况下创建响应并仅返回部分模板?
额外的数据
[#<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>]
,这是完整的响应。
<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 class="absolute inset-y-0 right-0 flex items-center pr-4 text-indigo-600">
<svg class="h-5 w-5" 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 class="ml-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 class="absolute inset-y-0 right-0 flex items-center pr-4 text-indigo-600">
<svg class="h-5 w-5" 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 class="ml-2 truncate text-gray-500">
<%= user.email %>
</span>
</li>
<% end %>
非常感谢。
Im playing around Rails 7 + Turbo and I'm trying to create a form which has a dropdown of user lists. The form could create a new "Unit" record and assign users to this unit. The plan create a dropdown where you can search and select the owner of the unit record. The problem is turbo stream returns an extra data(the collection). I've checked the network request and apparently, the back-end is sending this template with extra data. How do i create a response without this extra data and just return the partial template?
the extra data
[#<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>]
and this is the full response.
<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 class="absolute inset-y-0 right-0 flex items-center pr-4 text-indigo-600">
<svg class="h-5 w-5" 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 class="ml-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>
this is the user controller
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
the partial is this
<%= @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 class="absolute inset-y-0 right-0 flex items-center pr-4 text-indigo-600">
<svg class="h-5 w-5" 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 class="ml-2 truncate text-gray-500">
<%= user.email %>
</span>
</li>
<% end %>
Thank you so much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
替换
将部分的开头
为,因为您不希望将
each
调用的返回值(即迭代器本身)呈现到视图中。请注意需要删除的=
。Replace
at the beginning of your partial with
because you do not want to render the return value of the
each
call, which is the iterator itself, into the view. Note the=
that needs to be removed.