Ruby in javascript

Hey Champs..

Can we write some ruby code into javascript ?

Can you guys light me?

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

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 .. :slight_smile: 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..

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 .. :slight_smile: 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

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..

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

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..

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

Colin

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.

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:

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

Fred

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 :stuck_out_tongue: ). 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