开发者

Rails Array to JSON

开发者 https://www.devze.com 2023-03-18 14:31 出处:网络
I have a rails array that is being populated from items in the database based on parameters from the query string. I then want to make that array into JSON to work the jquery full calendar plugin but

I have a rails array that is being populated from items in the database based on parameters from the query string. I then want to make that array into JSON to work the jquery full calendar plugin but i can't seem to get it to work.

Im am outputting json but its surrounded by [] and i don't know if this is what is stopping the calendar events from showing up in the jquery full calendar plugin.

def get_sessions
  @sessions = Session.find(:all, :conditions => ["starttime >= '#{Time.at(params['start'].to_i).to_formatted_s(:db)}' and endtime <= '#{Time.at(params['end'].to_i)开发者_StackOverflow.to_formatted_s(:db)}'"] )
  sessions = [] 
  @sessions.each do |session|
    sessions << {:id => session.id, :title => session.client.to_s, :session_num => session.session_num, :start => "#{session.starttime.iso8601}", :end => "#{session.endtime.iso8601}"}
  end
  render :json => sessions
end

This code is originally from the http://vinsol.com/blog/2010/03/29/jquery-full-calendar-with-rails/ rails 2 app but im trying to build it with rails 3.

Any help would be really appreciated.

Thanks


Have you tried render sessions.to_json ?


In the past i have found that it works quite well to do this instead...

render :text => sessions.to_json

Then in your javascript file, you can do this:

eval("var data = " + returnedData)

its probably not the 'best' practice, but it should work and you dont have to do anything special with routes.

0

精彩评论

暂无评论...
验证码 换一张
取 消