[Rails] Re: How to stop ruby escaping javascript?
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-javascript
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
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home