Rails 根据集合大小多次渲染部分
我有一个部分,它接受一个集合,迭代该集合以显示各个项目。
下面是代码:
部分:
<% for point in @points %>
<div id="im" style="float:left;width:80px;height:90px;padding:5px;border:solid 1px #D3D3D3; margin:5px; ">
<img width="80" height="80" src="\uploaded\<%= point.id %>.gif" />
<%= link_to_remote " Delete", :url => { :action => "delete_point", :id => point.id }, :before => "Element.show('spinner')", :complete => "Element.hide('spinner')" %>
</div>
<% end %>
来自控制器的 rjs:
page.replace_html :points_partial, :partial => 'points', :collection=>@points
出于某种原因,部分是根据集合中的项目数量呈现的。如果集合中有十个项目,则部分内容将被渲染一次。
这家伙也有类似的问题,但与布局有关。
在 Ruby on Rails 中渲染部分集合正在成倍增加items
这让我发疯,因为它应该很简单,并且所有其他部分都可以毫无困难地工作。
I have a partial that takes a collection, iterates through that collection displaying the individual items.
Here is the code:
The Partial:
<% for point in @points %>
<div id="im" style="float:left;width:80px;height:90px;padding:5px;border:solid 1px #D3D3D3; margin:5px; ">
<img width="80" height="80" src="\uploaded\<%= point.id %>.gif" />
<%= link_to_remote " Delete", :url => { :action => "delete_point", :id => point.id }, :before => "Element.show('spinner')", :complete => "Element.hide('spinner')" %>
</div>
<% end %>
The rjs from the controller:
page.replace_html :points_partial, :partial => 'points', :collection=>@points
For some reason the partial is rendered by the amount of items in the collection. If there are ten items in the collection then the partial is rendered then times.
This guy had a similar problem but it was related to layouts.
Render partial in Ruby on rails a collection is multiplying items
It is driving me mad because it should be simple and all the other partials work without any difficulty.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
:collection
使控制器迭代 @points 并为集合中的每个项目渲染一次部分内容。如果您将部分设置为仅渲染一个点,则控制器代码应该按预期工作。顺便说一句,在 Rails 3 中,您可以使用
<%= render @points %>
作为<%= render :partial =>; 的快捷方式。 "点/点", :collection => @points%>
The
:collection
causes the controller to iterate over @points and render the partial once for each item in the collection. If you set up the partial to render just one point, the controller code should work as expected.BTW, in Rails 3 you can use
<%= render @points %>
as a shortcut for<%= render :partial => "points/point", :collection => @points %>
:collection
参数告诉部分“显示@points
的每个成员的部分”,然后您的部分再次迭代同一个集合。在您的部分中,只需摆脱
for point in @points
循环的包装,它应该可以正常工作。请参阅此处了解更多信息:http://guides.rubyonrails.org/layouts_and_rendering.html#rendering -集合
the
:collection
parameter is telling the partial "display the partial for each member of@points
", and then your partial iterates again through the same collection.In your partial, just get rid of the wrapping
for point in @points
loop and it should work fine.See here for more info: http://guides.rubyonrails.org/layouts_and_rendering.html#rendering-collections