如何将 to_sentence 添加到 :collection

发布于 2024-12-07 01:40:12 字数 611 浏览 0 评论 0原文

我有以下代码可以生成最高级列表:

<%= 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

囍笑 2024-12-14 01:40:12

哦哦,现在我明白了。抱歉造成混乱。这就是我会做的:

在你的控制器中:

@superlative_bodies = @profile.superlatives.map &:body
# Equivalent to: @superlative_bodies = @profile.superlatives.map {|sup| sup.body }

在你的视图中:

= @superlative_bodies.to_sentence

有些人会在视图中完成这一切,这取决于你:

= @profile.superlatives.map(&:body).to_sentence

解释一下, .map 是一个超级有用的 Ruby 方法它接受一个数组或其他 Enumerable 和一个块,并返回一个新数组,其中每个元素都是应用该块后原始数组中的相应元素。例如:(

[ 'foo', 'bar', 'baz' ].map {|word| word.upcase } # => [ 'FOO', 'BAR', 'BAZ' ]
# or
[ 'foo', 'bar', 'baz' ].map &:upcase              # => [ 'FOO', 'BAR', 'BAZ' ]

当您只想在每个元素上调用相同的单个方法时,后者只是前者的缩短版本。)

Ohhh, now I understand. Sorry for the confusion. Here's what I would do:

In your controller:

@superlative_bodies = @profile.superlatives.map &:body
# Equivalent to: @superlative_bodies = @profile.superlatives.map {|sup| sup.body }

In your view:

= @superlative_bodies.to_sentence

Some people would do this all in the view instead, which is up to you:

= @profile.superlatives.map(&:body).to_sentence

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:

[ 'foo', 'bar', 'baz' ].map {|word| word.upcase } # => [ 'FOO', 'BAR', 'BAZ' ]
# or
[ 'foo', 'bar', 'baz' ].map &:upcase              # => [ 'FOO', 'BAR', 'BAZ' ]

(The latter is just a shortened version of the former for when you only want to call the same single method on every element.)

溺渁∝ 2024-12-14 01:40:12

也许是这样的?

module ProfilesHelper
  # ...
  def superlatives_items (profile)
    @@acb ||= ActionController::Base.new # required to access render_to_string
    profile.superlatives.collect |superlative|
      acb.render_to_string :partial => 'path/to/partial/superlative',
                           :layout => false,
                           :locals => { :superlative => superlative }
    end
  end
  # ...
end

# In view:

# <%= raw(superlatives_items(@profile).to_sentence) %>

Something like this, perhaps?

module ProfilesHelper
  # ...
  def superlatives_items (profile)
    @@acb ||= ActionController::Base.new # required to access render_to_string
    profile.superlatives.collect |superlative|
      acb.render_to_string :partial => 'path/to/partial/superlative',
                           :layout => false,
                           :locals => { :superlative => superlative }
    end
  end
  # ...
end

# In view:

# <%= raw(superlatives_items(@profile).to_sentence) %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文