Created
July 27, 2011 08:57
-
-
Save bonyiii/1108959 to your computer and use it in GitHub Desktop.
how convert a rails3 model to json for most jquery plugins
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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