javascript - Proper JSON formatting in Ajax -


i have simple friendship model allow users connect 1 another:

class friendship < activerecord::base  belongs_to :sender, class_name: "user"  belongs_to :receiver, class_name: "user" end 

in user model have method defined:

 def self.create_friendship(receiver)   friendship.create(sender_id: current_user.id, receiver_id: receiver)  end 

my user index view has simple javascript:

<script>  $("#searchbutton").click(function(){   var tod = $("#timeofday").val()   console.log(tod);   var dow = $("#dayofweek").val()   console.log(dow);    queries = {'time': tod , 'day': dow }   $("#myselect").html("");   $.ajax({     url:'/users.json',     type:'get',     data: {search: queries},     datatype: 'json',     success: function( json ) {     //console.debug(json)     $.each(json.users, function(index, value) {       $('#myselect').append($('<option>').text(value.name).attr('value', value.id));     });   } });  })  $('#addbutton').click(function(){   var x = $("#myselect").val()    receiver = {} })   </script> 

what's confusing me best way format json data plugged user create method. appreciated.

i think best way use form tags remote: true. automatically format data properly. more that, default rails checks csrf_token in non-get queries , form tags automatically include token.

to handle form reactions different events can use ujs: list of events fired on form.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -