2010-03-30 4 views

risposta

8

Stai usando Rails 3 Beta? Rails 2 per impostazione predefinita non esegue l'escape dell'output HTML, in genere è necessario utilizzare l'helper h, vedere il post di Nate. Se si utilizza Rails 3, è necessario utilizzare l'helper raw o impostare la stringa come sicura per HTML. Esempi

<% my_variable = "<b>Some Bolded Text</b>" %> 
<%= raw my_variable %> 

O

<% my_variable = "<b>Some Bolded Text</b>".html_safe %> 
<%= my_variable %> 

Controlla la tua versione di Rails e ottenere di nuovo a noi.

+0

Grazie Scott! Era così! Sto utilizzando la Beta Rails 3 – deadkarma

+0

la parola chiave "raw" ha funzionato per me anche su rails 4 – emery

0

ActionView::Helpers::TextHelper fornisce un metodo strip_tags, che invece di fuggire i tag, li rimuove completamente.

fonte [riferimento]:

def strip_tags(html)  
    return html if html.blank? 
    if html.index("<") 
     text = "" 
     tokenizer = HTML::Tokenizer.new(html) 
     while token = tokenizer.next 
     node = HTML::Node.parse(nil, 0, 0, token, false) 
     # result is only the content of any Text nodes 
     text << node.to_s if node.class == HTML::Text 
     end 
     # strip any comments, and if they have a newline at the end (ie. line with 
     # only a comment) strip that too 
     text.gsub(/<!--(.*?)-->[\n]?/m, "") 
    else 
     html # already plain text 
    end 
    end 

<%= strip_tags(my_variable) %> 
+0

Grazie solo, ma mi piacerebbe che i tag HTML fossero intatti e resi nella pagina – deadkarma