Forum: Ruby on Rails How to stop ruby escaping javascript?

Posted by Sam Pei (sampei)
on 2012-11-14 18:25
Hi all.

If In my view I have:

<%= text_field_tag('name','',:onfocus => "for (var i=1;i<99999;i++){};"
%>

I'd expect to have this html:

 <input  name="name" onfocus="for (var i=1;i<99999;i++){};" />

instead I have:

<input  name="name" onfocus="for (var i=1;i&lt;99999;i++){};" />

And obviously the javascript code does not work...

I also tried this:

<%= text_field_tag('name','',:onfocus => "for (var
i=1;i<99999;i++){};".html_safe %>

and this:

<%= text_field_tag('name','',:onfocus => escape_javascript("for (var
i=1;i<99999;i++){};") %>

but the output is the same

Thanks to all.

PS: rails 2.3.8 ruby 1.9.2
Posted by Max Shytikov (Guest)
on 2012-11-14 19:26
(Received via mailing list)
Hi

Only one solution you should use method  'tag' directly
https://github.com/rails/rails/blob/39b9c943b7ec51...

Because escape = true by default for
'text_field_tag' 
https://github.com/rails/rails/blob/c091fae21f3225...

P.S: Do not use inline JS .
Posted by Sam Pei (sampei)
on 2012-11-15 12:35
Max Shytikov wrote in post #1084457:
> Only one solution you should use method  'tag' directly
> 
https://github.com/rails/rails/blob/39b9c943b7ec51...

Thank you, This works!

But finally I ended making a

getElementById("name").onfocus = ...

on load instead of changing all my text_field_tag with tag

> P.S: Do not use inline JS .
You're right, but my problem is that I have ruby code in rhtml that 
generates css and javascript codes. These codes are not static but 
depend on ruby variables values.

I can't figure out how to pass a ruby variable into an external css or 
js file...

So I have to keep all the code in one big (sigh!) rhtml...
Posted by Robert Walker (robert4723)
on 2012-11-15 17:37
Sam Pei wrote in post #1084545:
> Max Shytikov wrote in post #1084457:
> I can't figure out how to pass a ruby variable into an external css or
> js file...
>
> So I have to keep all the code in one big (sigh!) rhtml...

The simplest way is to not pass the data to JavaScript, but rather get 
the value from the DOM using JavaScript/jQuery. This is accomplished by 
using HTML5 data attributes.

Say you have a Ruby variable x:

HTML
------------
<div id="my_div" data-x="my_ruby_value">My content</div>

JavaScript
------------
function myFunction() {
  var x = $('#my_div').data("x");
  // Do something with x
}

Ryan Bates did a Railscasts episode on a few techniques to keep your 
JavaScript unobtrusive and yet still have access to your Ruby data:

http://railscasts.com/episodes/324-passing-data-to...
Posted by Sam Pei (sampei)
on 2012-11-15 20:39
Robert Walker wrote in post #1084592:
> Sam Pei wrote in post #1084545:
>> Max Shytikov wrote in post #1084457:
>> I can't figure out how to pass a ruby variable into an external css or
>> js file...
>>
>> So I have to keep all the code in one big (sigh!) rhtml...
>
> The simplest way is to not pass the data to JavaScript, but rather get
> the value from the DOM using JavaScript/jQuery. This is accomplished by
> using HTML5 data attributes.
>
> Say you have a Ruby variable x:
>
> HTML
> ------------
> <div id="my_div" data-x="my_ruby_value">My content</div>
>
> JavaScript
> ------------
> function myFunction() {
>   var x = $('#my_div').data("x");
>   // Do something with x
> }
>
> Ryan Bates did a Railscasts episode on a few techniques to keep your
> JavaScript unobtrusive and yet still have access to your Ruby data:
>
> http://railscasts.com/episodes/324-passing-data-to...

Thank you, I am doing so.

But $('#my_div').data("x") is not working form me, I had to do this:

HTML:
<div id="my_div" data-x='<%= my_ruby_var %>'>

JS:
 var div=document.getElementById('my_div');
 var x = div.getAttribute("data-x");

Btw, I think I could remove all Javascript code by rhtml by using this.

But what about css? Could something like that be done with css?

I have inline css with the :style attribute where I put my ruby 
variable:
(...)
:style => "width:" + my_ruby_var +  "px"
(...)

If i want to do a separate css file, how can I access to this ruby 
variable from inside the .css ?

Thank You
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.