0

I'm using ajax to check validate in client side, but i don't know how to get value in array of ruby on rails. Here is my ajax:

<script>
    $(document).ready(function() {
        $("#button").click(function() {
            var email = $("#email").val();
            var name = $("#username").val();
            var phonenumber = $("#phonenumber").val();
            var pass = $("#password").val();
            var names = $("divname").toString();
            console.log(names);
            ....
        });
    }); 
</script>

When i check Console.log, I just see "Object" but not values of array:

 "[object Object]"

Here is my array, call by ruby on rails:

<% @usernames.each do |t| %>
<div id="divname"><%= t.username %></div>
<%end%>

Here my coontroller:

def index
    @usernames = User.find(:all, :select => "username")
    @user = User.create(:username => params[:username], :password => params[:password],
                        :email => params[:email], :phonenumber => params[:phonenumber])
    if @user 
      render 'index'
    else
      render 'index'
    end

So, please! help me to fix that :)

1 Answer 1

1

$("divname") is an jQuery object, so the result of toString() is "[object Object]".

id should be unique, use class instead.

<% @usernames.each do |t| %>
  <div class="divname"><%= t.username %></div>
<%end%>

Then in js:

var names = $(".divname").map(function() {
  return $(this).text();
}).get();
console.log(names);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.