我可以在Rails中使用link_to制作一系列链接吗? [英] Can I make an array of links using link_to in Rails?

查看:97
本文介绍了我可以在Rails中使用link_to制作一系列链接吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要显示的url/title对值列表. (更具体地说,每个对象都有自己的链接列表,其中一些带有0,一些带有1,有些带有更多.)我希望它们出现在以逗号分隔的列表中.所以我在.erb文件中写了这个:

I have a list of values in url/title pairs that I'd like to display. (More specifically, each object has its own list of links, some with 0, some with 1, some with more.) I would like them to appear in a list that is comma-separated. So I wrote this in my .erb file:

<%= links.map{|wl| link_to wl.title, wl.url}.join(', ') %>

让我惊讶的是,这显示了一个逗号分隔的我要创建的链接的HTML代码列表;也就是说,它将所有尖括号括起来并用&符号编码.为了确保高阶函数没有什么可笑的,我尝试了一个更命令性的版本:

Somewhat to my surprise, this displayed a comma-separated list of the HTML code for the links I wanted to create; that is, it was taking all the angle brackets and ampersand-encoding them. Just to make sure there wasn't anything funny in the higher-order functions, I tried a more imperative version:

<% a = [] %>
<% links.each do |wl| %>
  <% a << link_to(wl.title, wl.url) %>
<% end %>
<%= a.join(', ') %>

当然具有相同的结果.但是我不认为我误用了link_to,因为如果我将其修改为

with, of course, the same result. But I don't think I'm misusing link_to, because if I modify that to

<% links.each do |wl| %>
  <%= link_to(wl.title, wl.url) %>,
<% end %>

然后它实际上创建了链接.正是我想要的,几乎是 ,除了最后一个逗号之后还有一个逗号. link_to是否在引擎盖下进行了一些魔术处理,使其根据输出方向移动到不同的位置?有什么办法可以绕开那个魔法吗? join语义正是我在这里想要的,而且我显然可以弄清楚如何进行自己的滚动(我猜是使用each_index),但是对于必须是一个常见问题,这似乎是一种沉重且非Ruby的解决方案

then it actually creates links. It's almost exactly what I want, except that there's an extra comma after the last one. Is there some magic going on under the hood with link_to that makes it act differently depending on where its output is heading? Is there any way to bypass that magic? The join semantics would be exactly what I want here, and I can obviously figure out how to roll my own (using each_index, I guess) but it seems like an awfully heavy and un-Ruby solution to what must be a common problem.

推荐答案

虽然使用html_safe的建议将获得您想要的结果,但我建议您不要在您的视图中坚持这样的循环.在rails中有一种非常好的方法来完成您要寻找的事情:

While the suggestions to use html_safe will get the result you are going for, I would suggest you not stick loops like that in your view. There is a perfectly good way in rails to do what you are looking for:

在您当前具有循环的主视图文件中,将其更改为:

In your main view file where you currently have your loop change it to this:

<%= render partial: 'links/link', collection: links, spacer_template: 'shared/comma' %>

然后创建部分links/_link.html.erb

<%= link_to link.title, link.url %>

然后创建部分shared/_comma.html.erb

,

这种方法的好处是,您不必担心链接内容实际上是否安全,并且如果您选择以其他方式列出或设置样式,就像更改一个或两个模板一样简单.例如,如果您决定将其垂直而不是逗号,并在间隔模板中使用<br>而不是,.

The nice thing about this approach is you don't have to worry if your link content is actually safe and if you choose to list or style it another way it's as simple as changing one or both of the templates. For example if instead of commas you decide you want it vertical and use <br> instead of , in your spacer template.

这篇关于我可以在Rails中使用link_to制作一系列链接吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆