# Ruby in javascript

**URL:** <https://discuss.rubyonrails.org/t/ruby-in-javascript/50520>\
**Category:** rubyonrails-talk\
**Created:** [March 30, 2010, 11:25am UTC](https://discuss.rubyonrails.org/t/ruby-in-javascript/50520 "2010-03-30T11:25:56Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [March 30, 2010, 11:25am UTC](https://discuss.rubyonrails.org/t/ruby-in-javascript/50520/1 "2010-03-30T11:25:56Z")

</div>

Hey Champs..

Can we write some ruby code into javascript ?

Can you guys light me?

---

<div class="post-metadata">

**Author:** ![Colin\_Law1](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Colin\_Law1](https://discuss.rubyonrails.org/u/Colin_Law1)\
**Post date:** [March 30, 2010, 11:44am UTC](https://discuss.rubyonrails.org/t/ruby-in-javascript/50520/2 "2010-03-30T11:44:38Z")

</div>

Do you mean that within some javascript can you use Ruby? If you mean that then this would be difficult. The ruby code is run in the server which is where ruby is installed. Javascript is run in the users PC in the browser.

Colin

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [March 30, 2010, 12:07pm UTC](https://discuss.rubyonrails.org/t/ruby-in-javascript/50520/3 "2010-03-30T12:07:44Z")

</div>

Colin Law wrote:

> Do you mean that within some javascript can you use Ruby? If you mean that then this would be difficult. The ruby code is run in the server which is where ruby is installed. Javascript is run in the users PC in the browser.

Hi Colin, only u come when i asks anybody for help .. 🙂 Thanks.

Anyway my situation is that i have a gem installed on my machine "calender\_date\_select" and i am dynamically populating the fields using javascript.. Now i also need to create a text-box with calendar with javascript.. Ordinarily, this is the way we use calendar\_date\_select:- \<%= calendar\_date\_select :abc, :def, :year\_range =\> 1.years.ago..1.years.since, %\>

Can we write it in javascript..

---

<div class="post-metadata">

**Author:** ![Colin\_Law1](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Colin\_Law1](https://discuss.rubyonrails.org/u/Colin_Law1)\
**Post date:** [March 30, 2010, 12:37pm UTC](https://discuss.rubyonrails.org/t/ruby-in-javascript/50520/4 "2010-03-30T12:37:56Z")

</div>

> Colin Law wrote:
> 
> > Do you mean that within some javascript can you use Ruby? If you mean that then this would be difficult. The ruby code is run in the server which is where ruby is installed. Javascript is run in the users PC in the browser.
> 
> Hi Colin, only u come when i asks anybody for help .. 🙂 Thanks.

Glad to be of help

> Anyway my situation is that i have a gem installed on my machine "calender\_date\_select" and i am dynamically populating the fields using javascript.. Now i also need to create a text-box with calendar with javascript.. Ordinarily, this is the way we use calendar\_date\_select:- \<%= calendar\_date\_select :abc, :def, :year\_range =\> 1.years.ago..1.years.since, %\>

You can do anything you like in javascript that you can do on any web page. What do you mean by "create a text-box with calendar".

Colin

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [March 31, 2010, 5:43am UTC](https://discuss.rubyonrails.org/t/ruby-in-javascript/50520/5 "2010-03-31T05:43:19Z")

</div>

While using calendar\_date\_select, it appers like this .. i mean usage of this gem..

\<%= calendar\_date\_select :abc, :def, :year\_range =\> 1.years.ago..1.years.since %\>, it come like this

TEXT\_BOX(nameabc[def]) Button

After clicking on button a calendar popups and whatever date i select comes in the text box.. So in web page i am doing it like this only but how should i do this in javascript.. I am little bit confused..

---

<div class="post-metadata">

**Author:** ![Colin\_Law1](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Colin\_Law1](https://discuss.rubyonrails.org/u/Colin_Law1)\
**Post date:** [March 31, 2010, 7:48am UTC](https://discuss.rubyonrails.org/t/ruby-in-javascript/50520/6 "2010-03-31T07:48:06Z")

</div>

I have not used calendar\_date\_select but I imagine it is already using javascript to do the popup. Have a look at the html of your page and see what is there. (View, Page Source or similar in browser).

Colin

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [March 31, 2010, 8:04am UTC](https://discuss.rubyonrails.org/t/ruby-in-javascript/50520/7 "2010-03-31T08:04:56Z")

</div>

Colin Law wrote:

> > how should i do this in javascript.. I am little bit confused..
> 
> I have not used calendar\_date\_select but I imagine it is already using javascript to do the popup. Have a look at the html of your page and see what is there. (View, Page Source or similar in browser).
> 
> Colin

Source code for line

\<%= calendar\_date\_select :abc, :def, :year\_range =\> 1.years.ago..1.years.since, %\>

is as:-

\<input id="abc\_def" type="text" value="" size="30" name="abc[def]"/\> \<img class="calendar\_date\_select\_popup\_icon" style="border: 0px none ; cursor: pointer;" src="/abc/images/calendar\_date\_select/calendar.gif?1266227981" onclick="new CalendarDateSelect( $(this).previous(), {year\_range:[2009, 2011]} );" alt="Calendar"/\>

How should i write this code in javascript.. I tried it but fails..

---

<div class="post-metadata">

**Author:** ![Colin\_Law1](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Colin\_Law1](https://discuss.rubyonrails.org/u/Colin_Law1)\
**Post date:** [March 31, 2010, 8:30am UTC](https://discuss.rubyonrails.org/t/ruby-in-javascript/50520/8 "2010-03-31T08:30:39Z")

</div>

I don't understand what you mean when you say you want to write it in javascript.

Colin

---

<div class="post-metadata">

**Author:** ![Rui\_Carvalho](https://avatars.discourse-cdn.com/v4/letter/r/c0e974/32.png) [@Rui\_Carvalho](https://discuss.rubyonrails.org/u/Rui_Carvalho)\
**Post date:** [March 31, 2010, 8:38am UTC](https://discuss.rubyonrails.org/t/ruby-in-javascript/50520/9 "2010-03-31T08:38:31Z")

</div>

Hello,

if you define your Javascript function in your “html.erb” (and not on a external file) you could just use \<%= … %\> to access ruby predefined variables and on ruby you would write that code in to a string and then use it.

but the calendar\_date\_select controle pretty much covers all the javascript needed for it to work…you just have to put it one the page.

regs.

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [March 31, 2010, 9:29am UTC](https://discuss.rubyonrails.org/t/ruby-in-javascript/50520/10 "2010-03-31T09:29:20Z")

</div>

Thanks colin and Rui..

I made it worked.. I copied the code from page source and give it to the onclick of the text box. Earlier also, i was donig the same but a variable was not there. So fixed it and worked like a charm..

Thanks again..

Colin Law wrote:

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [March 31, 2010, 10:33am UTC](https://discuss.rubyonrails.org/t/ruby-in-javascript/50520/11 "2010-03-31T10:33:48Z")

</div>

I doubt this is what was being asked but there is a an implementation of ruby in javascript (hotruby)

Fred

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [March 31, 2010, 5:40pm UTC](https://discuss.rubyonrails.org/t/ruby-in-javascript/50520/12 "2010-03-31T17:40:51Z")

</div>

Hemant Bhargava wrote:

> Hey Champs..
> 
> Can we write some ruby code into javascript ?
> 
> Can you guys light me?

you can make a main\_scripts.js.erb inside one of your views

then you can mix in ruby and JS together. The ruby will be processed first, and then made into a static js file, then you can include that. OR, you can do inline JS in your views (it's yucky though 😛 ). You could also get crazy and include the Johnson gem and then get some crazy ruby generating JS on the fly stuff.

Good luck!

~Jeremy
