Skip to content

Instantly share code, notes, and snippets.

@bonyiii
Created July 27, 2011 08:57
Show Gist options
  • Select an option

  • Save bonyiii/1108959 to your computer and use it in GitHub Desktop.

Select an option

Save bonyiii/1108959 to your computer and use it in GitHub Desktop.
how convert a rails3 model to json for most jquery plugins
#http://asciicasts.com/episodes/258-token-fields
@authors = Author.all
respond_to do |format|
format.html
format.json { render :json => @authors.map(&:attributes) }
end
or (jqgrid)
Menu.include_root_in_json = false
json_data = @menus.to_json(:methods => [ :level, :is_leaf ])
#json_data = @menus.to_json()
Menu.include_root_in_json = true
respond_to do |format|
format.json {
render :text => '{
"totalpages" : "1",.
"currpage" : "1",
"totalrecords" : ' + @menus.size.to_s + ',
"menudata" : ' + json_data +'}'
}
format.html # index.html.erb
format.xml { render :xml => @menus }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment