Reputation: 61
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 :)
Upvotes: 0
Views: 210
Reputation: 160973
$("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);
Upvotes: 1