2

I want to access a Ruby array in javascript. Please tell me the method to do that. My array is holding the result of a sql query.

  @contacts = Contact.order("contacts.position ASC")

I am trying to do this....

for(var i=0; i< a; i++)
    {   
        var firstnameV = "<%=Contact.order('contacts.position ASC')[i].first_name%>";
        var lastnameV = "<%=Contact.order('contacts.position ASC')[i].last_name%>";
        var emailV = "<%=Contact.order('contacts.position ASC')[i].email%>";
        var contactV = parseInt("<%=Contact.order('contacts.position ASC')[i].contact_no%>";
        var posV = parseInt("<%=Contact.order('contacts.position ASC')[i].position%>";  
        tx.executeSql('INSERT INTO contact_table (firstname, lastname, email, contact, pos)
        VALUES (firstnameV,lastnameV, emailV, contactV, posV)');
    }
2
  • 2
    Just render json and access it through ajax Commented Feb 3, 2011 at 8:44
  • 1
    @apneadiving Could you please show the steps. I am totally new at rails and ajax. Commented Feb 3, 2011 at 9:07

3 Answers 3

4

Quick example of how you can render the value of Ruby variable to JavaScript. Add <%= yield :head %> to head tag in views/layouts/application.html.erb. Then in views/contacts/index.erb (or whatever view you use) add the following:

<%content_for :head do %>
<script type="text/javascript">
window.onload = function() {
    alert("First contact in database is <%=Contact.order('contacts.position ASC').first.name%>")
}
</script>
<%end%>

This will alert the first contact name from your database.

Sign up to request clarification or add additional context in comments.

9 Comments

@Nikita: How am I suppose to select a particular element for the array. For eg.When I try to do this <%=Contact.order('contacts.position ASC')[i].first.name%> it shows error. A constant in the place of 'i' works fine. The error is undefined local variable i.
@dilip - OK, it gets a bit more advanced. <% somecode %> in view executes ruby code. <%= ruby variable %> (note = sign) inserts value of ruby variable to the html code in view. So one option for you can be constructing a loop and then inserting it to your view with <% %>
@nikita: I know how to add ruby variables to html code. But here i want to assign ruby variable to javascript variable. I tried doing this var first = "<%=Contact.order('contacts.position ASC')[i].first_name%>"; But it works only if i give a contant number in the place of i. Else it shows local variable undeclared.
Exactly. var first = is outside <% %> so it will be parsed as a part of html code, not as Ruby code. Use something like <% i = 5 %><%=Contact.order('contacts.position ASC')[i].first_name%> or even a loop, depending what you want.
<% @contacts = Contact.order("contacts.position ASC") %><%a = 10%><% for i in 0 do %>var firstname = <%=@contacts[i].first_name%><%end%>
|
1

You can do this by using the

to_json

method in Ruby

or

render :json => @contacts

1 Comment

After that how am I suppose to use it in javscript? I am totally new to rails.
0

Ruby is server side language. JavaScript is mostly (server side also - e.g. node.js) client side. If you want to pass values from Ruby to JS, you could render that value as part of view in script tags or retrieve them via AJAX.

1 Comment

@Tarscher gave you an example in his answer :) And I suggest reading about JSON: json.org

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.