Search code examples
ruby-on-railsruby-on-rails-4link-to

add links to comma separated list in rails


I am transferring an unordered list with links to a comma separated list with links. I can make the comma separated list fine...but I can't figure out how to get the links into the comma separated list.

BEFORE

ul.list-unstyled
  - @cats.each do |c|
    = link_to animal_path(id: c.id)
      li
        =c.breed

AFTER

= @cats.each.map{ |c| c.breed }.join(",  ") 

This is where I am lost.


Solution

  • Try this:

    @cats.map{ |c| link_to(c.breed,animal_path(c))}.join(", ").html_safe
    

    Your current setup will display a list of cat breeds, but is not linking to anything, hence the use of the link_to call. This will return as escaped html unless otherwise specified, so you can make a call to html_safe to convert the resulting anchor tags to clickable links.