如何将 to_sentence 添加到 :collection
我有以下代码可以生成最高级列表:
<%= render :partial => 'superlative', :collection => @profile.superlatives %>
上面引用的 :partial
代码如下:
<li class="superlative"><span title="<%= superlative.name %>">
<%= superlative.body %>
</span></li>
如何将 to_sentence
添加到 @profile.superlatives 集合?我尝试过:
<%= render :partial => 'superlative', :collection => @profile.superlatives.to_sentence %>
但是这样做会使@profile.superlatives从视图中消失。
我查看了文档但找不到答案。
I have the following code that generates a list of superlatives:
<%= render :partial => 'superlative', :collection => @profile.superlatives %>
The :partial
code referenced above is as follows:
<li class="superlative"><span title="<%= superlative.name %>">
<%= superlative.body %>
</span></li>
How can I add to_sentence
to the @profile.superlatives collection? I tried:
<%= render :partial => 'superlative', :collection => @profile.superlatives.to_sentence %>
Doing so however makes the @profile.superlatives disappear from the view.
I looked at the docs but couldn't find an answer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
哦哦,现在我明白了。抱歉造成混乱。这就是我会做的:
在你的控制器中:
在你的视图中:
有些人会在视图中完成这一切,这取决于你:
解释一下,
.map
是一个超级有用的 Ruby 方法它接受一个数组或其他 Enumerable 和一个块,并返回一个新数组,其中每个元素都是应用该块后原始数组中的相应元素。例如:(当您只想在每个元素上调用相同的单个方法时,后者只是前者的缩短版本。)
Ohhh, now I understand. Sorry for the confusion. Here's what I would do:
In your controller:
In your view:
Some people would do this all in the view instead, which is up to you:
To explain,
.map
is a super-useful Ruby method that takes an array or other Enumerable and a block and returns a new array where each element is the corresponding element from the original array after the block has been applied to it. For example:(The latter is just a shortened version of the former for when you only want to call the same single method on every element.)
也许是这样的?
Something like this, perhaps?