¿Cómo imprimir código JavaScript en mi vista?

I need to supply my users with js code, so I have text area:

  <%=  text_area_tag 'body',  "//<script>...</script>"%>

I can't make it working. I know how to do it with variables from controllers :

      @payKey = @xml["payKey"].to_s()
      "#{@payKey}"

EDIT. FUll text to print:

    <script>var _p_g={id:@website.id};</script>
   <script src="..."></script>

and print it in view, but gave me error:

   E:/myapp/myapp_prev/myapp/app/controllers/websites_controller.rb:12: unknown regexp option - j
   E:/myapp/myapp_prev/myapp/app/controllers/websites_controller.rb:12: syntax error, unexpected tSTRING_BEG, expecting keyword_end
   <></script>"
                                               ^

So what would be better way to solve this trouble ?

preguntado el 31 de julio de 12 a las 10:07

1 Respuestas

The problem appears to be that you're putting double quotes inside of other quotes, causing a syntax error.

Prueba esto:

 <%=  text_area_tag 'body',  '<script src="//cdn.printitgreen.com/js/embedded.js"></script>'%>

Respondido 31 Jul 12, 10:07

can you help me with this ? I need to print variable value : '<script>var _p_g={id:#{@website.id}};></script>' this only print #{@website.id}, but I need its value. - negar7ko

I need to use double quotes ? - negar7ko

Ok, you can escape double quotes using the escape_javascript ayudante: "<script>var _p_g={id:#{escape_javascript(@website.id)}};></script>" - Andrés Nesbitt

Prueba esto: "<script>var _p_g={id:@website.id};</script><script src=\"...\"></script>" putting a backslash infront of the extra double quotes instead of escaping the id. - Andrés Nesbitt

It doesn't give error, but also doesn't print website.id value. - negar7ko

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.