javascript - Rails 4/AJAX GET data from controller -


i posted poor question earlier, reposting , making mvce.

i'm building messaging service rails , ajax. far can submit message through form, update in html dom, ajax post method send controller, , controller save in database.

now need add ajax method message submitted -- other users (in other browsers) able view it.

currently, , hack job way of doing it, in js code set timeout calls ajax function every half second. there better way -- in, once controller saves message can call ajax function? ajax code looks this:

function retrievemessages(){   var message;   <%debugger%>   $.ajax({     type:"get",     url:"<%= messages_get_path %>",     datatype:"json",     data: { what_goes_here: "blah" }, //this part not understand -- see below     success:function(data){       message = data;       console.log(data)     }   });    settimeout(retrievemessages, 500); }  $(document).ready(function(){   //get messages   settimeout(retrievemessages, 500); ... more irrelevant 

the line data: { what_goes_here: "blah" } doesn't make sense me. syntax controller send data stored data:? furthermore, console can see what_goes_here being passed parameter controller -- again doesn't make sense me.

my route looks get 'messages/get', :to => 'messages#get' (this might incorrect?)

rake routes shows

  messages_get    /messages/get(.:format)        messages#get 

and of now, don't have in controller other respond_to because @ point i'm trying call controller. syntax send data ajax method?

def   debugger   respond_to |format|     format.html     format.json {render json: @variable} //is @variable being passed ajax call?   end end 

update

this makes more sense me... ajax method calls def get function. def get function finds message in database, , stores in instance variable. subsequently, can add javascript code insert dom. must have wrong in routing because i'm getting (in console) http://localhost:3000/messages/get 404 (not found)

what doing, suspect, not effective. more users online more load these refreshing requests, of them returning no new data.

you should consider more active way of notifying browsers changes on server. 1 option use actioncable.


Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -