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?
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.
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.
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.
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).
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(', ') + ".") %>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With