Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 4 raw html_safe not working

I am not able to escape raw html tags in the view

<% array =  @article.tags.collect { |p| (link_to p.name, '#') } %>
<%= array.join(' , ') %>

Raw and html_safe

<% array =  @article.tags.collect { |p| raw (link_to p.name, '#') } %>
<% array =  @article.tags.collect { |p| (link_to p.name, '#').html_safe } %>

Giving me this output, without escaping the html tags

<a href="#">tag1</a> , <a href="#">tag2</a> , <a href="#">tag4</a> 
like image 544
Ashwin Yaprala Avatar asked Apr 25 '13 14:04

Ashwin Yaprala


People also ask

What does Html_safe do in Rails?

html_safe actually "sets the string" as HTML Safe (it's a little more complicated than that, but it's basically it). This way, you can return HTML Safe strings from helpers or models at will. h can only be used from within a controller or view, since it's from a helper. It will force the output to be escaped.

What does HTML safe mean?

Safe HTML is a module that filter the input before the content is stored in the database. Unlike Drupal basic filtering system, Safe HTML filter the form post and perform code cleaning before the content is stored on the site backend.

What is raw in Ruby?

It is much easier to tell if a ruby is real when it is raw and uncut, for a variety of reasons. Rubies like to grow in a flat, hexagonal shape. If the uncut gem shows this natural growth characteristic, along with parts of its host rock (marble or alkali basalt) still attached, it is very likely it is a real ruby.


1 Answers

This solved my problem

<%= raw (@article.tags.map { |p| (link_to p.name, '#') }.join(' , ')) %>
like image 93
Ashwin Yaprala Avatar answered Oct 28 '22 18:10

Ashwin Yaprala