passing data from array of hash in ruby into javascript full calendar -
i trying pass value data @ index ruby :
in user.rb file :
list = [{:date=>"07 mar", :price => "1000,00", :fruit=>"orange"}, {:date=>"07 mar", :price => "2000,00", :fruit=>"apple"}] to js in full_calendar.js :
dayrender:function(date, cell){ var array = <%= j @list.to_json %>; $(cell).html('<span>' + array + '</span>'); }, in full calendar , can't make work. have tried lot of different options far , can't find solution access @ list 1 of value @ index 0, 1, or 2...
any ideas?
you need split server , client part , test each. first ruby: need this, test first in separate ruby script.
require 'json' @list = [{:date=>"07 mar", :price => "1000,00", :fruit=>"orange"}, {:date=>"07 mar", :price => "2000,00", :fruit=>"apple"}] @list.to_json # [{"date":"07 mar","price":"1000,00","fruit":"orange"},{"date":"07 mar","price":"2000,00","fruit":"apple"}] then client part: put yout ruby code in place can see result in javascript. make sure json required somewhere in line.
dayrender:function(date, cell){ var array = <%= require 'json' @list = [{:date=>"07 mar", :price => "1000,00", :fruit=>"orange"}, {:date=>"07 mar", :price => "2000,00", :fruit=>"apple"}] @list.to_json %>; console.log(array); $(cell).html('<span>' + array + '</span>'); }, if works, it's matter of testing if @list variable known in erb file. if not concentrate on passing variable, erb file in right path ? route correct ? etc.
dayrender:function(date, cell){ var array = <%=@list.to_json %>; $(cell).html('<span>' + array + '</span>'); }, as extra: make simple test script can extract problem, test without having bother other influences , above can publish here in case can't figure out. ask rails use sinatra of time, have same rails. in case don't know sinatra, put of in app.rb , run.
require 'sinatra' # gem install sinatra require "sinatra/reloader" #so don't have restart server after changes require 'json' '/' @list = [{:date=>"07 mar", :price => "1000,00", :fruit=>"orange"}, {:date=>"07 mar", :price => "2000,00", :fruit=>"apple"}] erb :index end __end__ @@ index <script type="text/javascript" src="./jquery.js"></script> <script> $(document).ready(function(){ var array = <%=@list.to_json%>; console.log(array); }); </script> <body> </body> and yes, json in console..
Comments
Post a Comment