0

html_safe and raw works good inside Viwe, but it dosent work inside action inside controller

a = "<p> sample text </p>"

Inside view <%=a.html_safe%>
give output "Sample text"

Inside controller

def test 

   a = "<p> sample text </p>" 
   a.html_safe 

end

this returns the as it is "<p> sample text </p>"

please guide me how to make this html_safe work inside controller action....

lamrin
  • 1,431
  • 4
  • 22
  • 34

1 Answers1

0

I guess you should simply do:

def test 
  @a = "<p> sample text </p>".html_safe 
end

Just tried myself, and it works properly. In my view I have

<%= @a %>
apneadiving
  • 114,565
  • 26
  • 219
  • 213
  • it returns "

    sample text

    "
    – lamrin Jan 02 '12 at 14:29
  • in works inside view... but when you inspect @a inside the controller it prints "

    sample text

    "
    – lamrin Jan 02 '12 at 15:02
  • actually iam using prawn gem to generate pdf, i am generating pdf from inside controller method, when i try to append a verialble with html_safe it dosent work – lamrin Jan 02 '12 at 15:07
  • Mmm ok, I understand better. Anyway, why don't you create a dedicated view for your pdf? it will be much easier to maintain as it would stick to mvc pattern – apneadiving Jan 02 '12 at 15:13
  • i have view for pdf; that works, but inside pdf file values are displayed with html tags – lamrin Jan 02 '12 at 15:27
  • ok, so last chance: do you make manipulation with your strings? Indeed `"#{@a}, #{@b}"` would not be html safe because `@b` isn't. – apneadiving Jan 02 '12 at 15:30
  • I dont do any manipulation, i fetch value directly from database, it comes as string with html tags, and assign it to a @variable. After this i append this variable to pdf.table – lamrin Jan 02 '12 at 15:34
  • Try to look this [question](http://stackoverflow.com/questions/8722925/rails-3-prawn-pdf-html-safe) – ksugiarto Aug 31 '13 at 14:45