简体   繁体   English

与link_to助手结合的Rails地图方法?

[英]Rails map method with link_to helper combined?

I have a model where I want all the records for the association , separated by 'and' and being displayed as a link ... So far I have a map or a link_to option in mind .... but I would like to combine both: 我有一个模型,我希望该关联的所有记录都用“和”分隔并显示为链接...到目前为止,我已经想到了一个映射或link_to选项....但是我想结合一下都:

My desired result is this: 我想要的结果是这样的:

name and name2 and name3 etc. each being displayed as a link. namename2name3等均显示为链接。

so 所以

name... and name... 名字...名字...

The association works fine and gives me all entries if I do 协会运作良好,如果我愿意,可以给我所有参赛作品

= model.associations.map(&:name).join(' and ') 

But 'map' does not work as a link. 但是“地图”不能用作链接。 Is it possible to somehow combine map and link_to ? 是否可以通过某种方式将maplink_to结合起来?

For link_to I would use ..: 对于link_to我将使用..:

-model.associations.each do |a|
  =link_to a.name, a

This obviously would not separate the association entries with an 'and'. 显然,这不会将关联条目分隔为“和”。 Any tip is highly appreciated. 任何提示都将受到高度赞赏。 Thanks. 谢谢。

Try something like 尝试类似

= model.associations.map { |a| link_to(a.name, a) }.join(" and ").html_safe

But, I'd divide the logic between action and view. 但是,我将逻辑划分为行动和观点。 In the action 在行动中

@items = model.associations

In the view 在视图中

= @items.map { |a| link_to(a.name, a) }.join(" and ").html_safe

The easiest way for what you are doing is to just add and after the =link_to a.name, a . 最简单的方法是在=link_to a.name, a之后添加=link_to a.name, a Since you are only outputting the data in a template, and not saving it in a variable for reuse later, there is no need for a map 由于您仅将数据输出到模板中,而不是将其保存在变量中以供以后重用,因此不需要映射

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM