SHARE
TWEET

Untitled

a guest Jul 5th, 2012 234 Never
  1. How to create a Wufoo-like (form builder) web application with JQuery and Rails?
  2. var todoList = {};
  3.  
  4. $.getJSON("/users/123/todolists/456.json", function(data) {
  5.   loadTodoList(data);
  6.   ...
  7. });
  8.  
  9. function loadTodoList(data) {
  10.   todoList = data.todoList;
  11. }
  12.  
  13. function saveTodoList() {
  14.   $.ajax({
  15.     type: 'POST',
  16.     url: "/users/123/todolists/456",
  17.     data: JSON.stringify({ todoList: todoList }),
  18.     contentType: 'application/json',
  19.     dataType: 'script', // could be "json", "html" too
  20.     beforeSend: function(xhr){
  21.       xhr.setRequestHeader("X-Http-Method-Override", "put");
  22.     }
  23.   });
  24. }
  25.        
  26. # TodoListsController
  27.   def show
  28.     @todolist = TodoList.find_by_id(params[:id], :include => [:tasks, ...])
  29.     respond_to do |format|
  30.       format.json do
  31.         render :json => @todolist.to_json
  32.       end
  33.     end
  34.   end
  35.  
  36.   # TodoList model
  37.   def to_json
  38.     super(:only => :name,
  39.           :include => { :tasks => { :only => [:name, :description, ...],
  40.                                     :include => ... }})
  41.   end
RAW Paste Data
Top