Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I make an array of links using link_to in Rails?

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(', ') %>

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(', ') %>

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 %>

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.

like image 550
blahedo Avatar asked Jul 10 '13 01:07

blahedo


3 Answers

Use safe_join combined with html_safe on the separator string.

Example:

<%= safe_join(links.map { |l| link_to(l.title, l.url) }, ", ".html_safe) %>
like image 185
Gerry Shaw Avatar answered Nov 08 '22 17:11

Gerry Shaw


Try

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

This instructs Rails to not escape the text.

like image 12
James Lim Avatar answered Nov 08 '22 17:11

James Lim


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' %>

Then create a partial links/_link.html.erb

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

Then create a partial shared/_comma.html.erb

,

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.

like image 6
Alex Peachey Avatar answered Nov 08 '22 17:11

Alex Peachey