Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use a rails link_to helper within a .map block such that the output is an actual link, and not the text representation of the HTML

I have this:

listed in <%= @product.categories.map{ |cat| raw(link_to(cat.name, category_path(cat))) }.join(', ') + "." %>

This is the output:

listed in <a href="/categories/1">Men</a>, <a href="/categories/2">Women</a>. 

It doesn't actually convert that to an actual link. It spits it out as text on the webpage.

How do I get that link_to helper to be displayed as a link, and not HTML, from within that block?

like image 543
marcamillion Avatar asked Dec 31 '12 01:12

marcamillion


People also ask

What is the Rails helper method that creates the HTML for links?

link_to is a Rails built in helper that helps generate an anchor tag. To understand what it does and how it actually works, let's build a similar helper ourselves. link_to is a helper method provided by Rails, but we can easily define a simple version of this helper ourselves.

How does Link_to work in Rails?

Rails is able to map any object that you pass into link_to by its model. It actually has nothing to do with the view that you use it in. It knows that an instance of the Profile class should map to the /profiles/:id path when generating a URL.

How do I link pages in Ruby on Rails?

open index. html. erb file in app>>views>>home folder use in atom editor. After you open index file now add following HTML link code in that file and end with <br /> tag.

What is Link_to?

method: symbol of HTTP verb - This modifier will dynamically create an HTML form and immediately submit the form for processing using the HTTP verb specified. Useful for having links perform a POST operation in dangerous actions like deleting a record (which search bots can follow while spidering your site).


1 Answers

you need to wrap the result of your map call with raw. This way you can tell rails that the string should be outputted directly into the template.

listed in <%= raw(@product.categories.map{ |cat| raw(link_to(cat.name, category_path(cat))) }.join(', ') + ".") %>
like image 60
Yves Senn Avatar answered Oct 05 '22 08:10

Yves Senn