【发布时间】:2013-02-19 00:03:45
【问题描述】:
我知道这应该很简单,但我是 ruby、haml 和 rails 的新手。
我想显示特定律师的地点列表。我想将它们转换为“to_sentence”,所以除了最后一个位置之外,每个位置后面都有逗号。
这就是我在 show.html.haml 视图中的内容。
%ul
- @lawyers.each do |lawyer|
- if lawyer == @lawyer
%li.active
%article
%h3= lawyer.full_name
- unless lawyer.phone.nil?
.phone== Direct #{lawyer.phone}
.email
= mail_to lawyer.email, lawyer.email
- unless @lawyer.office_locations.empty?
- if @lawyer.office_locations.count > 1
.locations_list
%ul
%li Locations:
- @lawyer.office_locations.each do |office_location|
%li.locations== #{office_location.city}
- else
.locations_list
%ul
%li Location:
- lawyer.office_locations.each do |office_location|
%li== #{office_location.city}
编辑: 这是我尝试过的。
- unless @lawyer.office_locations.empty?
- if @lawyer.office_locations.count > 1
.locations_list
%ul
%li Locations:
%li== lawyer.office_locations.collect{ |p| "#{p.office_locations.city}"}.to_sentence
我不知道这是否有所不同,但lawyer 和office_location 是两个独立的模型,它们都是HABTM。
我知道这是错误的做法,但我似乎无法让 to_sentece 或 .pluralize 工作。我尝试使用此帖子 Ruby on Rails seems to be auto-escaping html created by link_to 上的答案,但无法正常工作。
我假设我可能想把这个逻辑放在一个助手中?任何帮助将不胜感激。
【问题讨论】:
-
请发布不起作用的代码 - 在您的示例中我既没有看到
to_sentence也没有看到pluralize。
标签: ruby-on-rails ruby haml