Skip to content

Instantly share code, notes, and snippets.

@zacksheppard
Last active August 29, 2015 14:04
Show Gist options
  • Save zacksheppard/79f413dbb3af43f757ab to your computer and use it in GitHub Desktop.
Save zacksheppard/79f413dbb3af43f757ab to your computer and use it in GitHub Desktop.

Revisions

  1. zacksheppard revised this gist Jul 29, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion rails_ajax_forms.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    Notes from Flatiron School lecture w08.d04 showing different ways to us AJAX forms in Rails.
    Notes showing different ways to us AJAX forms in Rails.
    We will use a Todo list as an example domain for the examples.


  2. zacksheppard revised this gist Jul 29, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion rails_ajax_forms.md
    Original file line number Diff line number Diff line change
    @@ -76,7 +76,7 @@ Convention over configuration!
    $('ul').prepend("<%=j render @list %>")
    // it renders the @list object passed to it. By convention it looks for a 'list' partial to render.
    ```

    ----
    `views/lists/_list.html.erb`
    ```ruby
    <li>
  3. zacksheppard revised this gist Jul 29, 2014. 1 changed file with 5 additions and 2 deletions.
    7 changes: 5 additions & 2 deletions rails_ajax_forms.md
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@ We will use a Todo list as an example domain for the examples.


    ### Lowest level of abstraction
    ---------------------------------
    ----
    `views/lists/index.erb.html` This will create the list input form
    (__Note__: This should also work in Sinatra)
    ```ruby
    @@ -21,7 +21,7 @@ We will use a Todo list as an example domain for the examples.
    </ul>
    ```

    ---------------------------------
    ----
    `/assets/javascripts/lists.js` This will listen to the form, catch the request, send it to the create route and reply modifying the page.
    ```javascript
    // create a function to handle the form data
    @@ -51,6 +51,7 @@ remote: true is part of [rails/jquery-rails](https://github.com/rails/jquery-rai

    When you tell the form it is a remote form it adds what's known as the 'universal handler' which will select that form, bind an event to it's submit action, take off the original form envelope (the standard form submission), take the form data, put it in a new AJAX envelope, and send it out to the server using the original action.

    ----
    `views/index.erb.html` Create the list input form
    ```ruby
    # The 'remote: true' here is the key that makes this work.
    @@ -69,6 +70,7 @@ When you tell the form it is a remote form it adds what's known as the 'universa
    Convention over configuration!
    * If we were submitting a form for a nested resource (for a list task for instance) the form_for would instead look like this `<% form_for [@list, Task.new], remote: true do |f| %>` Because task is a nested resource, and rails knows to look for a POST route of `list/1/task` we just need to pass it which list and rails will automatically use that route.

    ----
    `views/lists/create.js.erb`
    ```javascript
    $('ul').prepend("<%=j render @list %>")
    @@ -83,6 +85,7 @@ $('ul').prepend("<%=j render @list %>")
    </li>
    ```

    ----
    `/assets/javascripts/lists.js` Notice that this file isn't here at all in this pattern
    ```javascript
    // The string injection is handled in the create.js.erb file
  4. zacksheppard revised this gist Jul 29, 2014. 2 changed files with 0 additions and 12 deletions.
    2 changes: 0 additions & 2 deletions form_tag_remote_note.md
    Original file line number Diff line number Diff line change
    @@ -1,2 +0,0 @@
    http://stackoverflow.com/questions/4227775/rails-form-for-remote-true-is-not-calling-js-method

    10 changes: 0 additions & 10 deletions rails_ajax_forms.md
    Original file line number Diff line number Diff line change
    @@ -89,13 +89,3 @@ $('ul').prepend("<%=j render @list %>")
    // The event handler has been abstracted away in remote: true
    // the post has also been abstracted away in remote: true. When using remote: true a new attribute to the form 'data-remote="true"'
    ```
    [continue at 2:27 from first vid]
    http://flatiron-videos.s3.amazonaws.com/ruby-005/remote-true-ajax.mp4
    http://flatiron-videos.s3.amazonaws.com/ruby-005/low-level-remote-true.mp4
    resources
    http://pcrglennon.github.io/blog/2014/07/20/starting-a-new-sinatra-app-from-scratch/
    http://guides.rubyonrails.org/working_with_javascript_in_rails.html
  5. zacksheppard revised this gist Jul 28, 2014. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions form_tag_remote_note.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    http://stackoverflow.com/questions/4227775/rails-form-for-remote-true-is-not-calling-js-method

  6. zacksheppard revised this gist Jul 28, 2014. 1 changed file with 7 additions and 7 deletions.
    14 changes: 7 additions & 7 deletions rails_ajax_forms.md
    Original file line number Diff line number Diff line change
    @@ -44,17 +44,17 @@ $(function(){
    ```
    ### AJAX form using `remote: :true`
    ### AJAX form using `remote: true`
    This uses a built in rails method called remote to let the form know it will be using a remote submission that isn't the norm.
    remote: :true is part of [rails/jquery-rails](https://github.com/rails/jquery-rails)
    remote: true is part of [rails/jquery-rails](https://github.com/rails/jquery-rails)

    When you tell the form it is a remote form it adds what's known as the 'universal handler' which will select that form, bind an event to it's submit action, take off the original form envelope (the standard form submission), take the form data, put it in a new AJAX envelope, and send it out to the server using the original action.

    `views/index.erb.html` Create the list input form
    ```ruby
    # The 'remote: :true' here is the key that makes this work.
    <% form_for List.new, remote: :true do |f| %>
    # The 'remote: true' here is the key that makes this work.
    <% form_for List.new, remote: true do |f| %>
    <input id="new-todo" placeholder="What needs to be done?" autofocus="" autocomplete="off">
    <% end %>
    @@ -67,7 +67,7 @@ When you tell the form it is a remote form it adds what's known as the 'universa
    * For this example the original action would be POST to the `def create` route with html and automatically get view/create.html.erb back.
    * It will now take the same POST to `def create` but since the request was in JS, it will look for view/create.js.erb instead.
    Convention over configuration!
    * If we were submitting a form for a nested resource (for a list task for instance) the form_for would instead look like this `<% form_for [@list, Task.new], remote: :true do |f| %>` Because task is a nested resource, and rails knows to look for a POST route of `list/1/task` we just need to pass it which list and rails will automatically use that route.
    * If we were submitting a form for a nested resource (for a list task for instance) the form_for would instead look like this `<% form_for [@list, Task.new], remote: true do |f| %>` Because task is a nested resource, and rails knows to look for a POST route of `list/1/task` we just need to pass it which list and rails will automatically use that route.

    `views/lists/create.js.erb`
    ```javascript
    @@ -86,8 +86,8 @@ $('ul').prepend("<%=j render @list %>")
    `/assets/javascripts/lists.js` Notice that this file isn't here at all in this pattern
    ```javascript
    // The string injection is handled in the create.js.erb file
    // The event handler has been abstracted away in remote: :true
    // the post has also been abstracted away in remote: :true. When using remote: :true a new attribute to the form 'data-remote="true"'
    // The event handler has been abstracted away in remote: true
    // the post has also been abstracted away in remote: true. When using remote: true a new attribute to the form 'data-remote="true"'
    ```
  7. zacksheppard revised this gist Jul 28, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion rails_ajax_forms.md
    Original file line number Diff line number Diff line change
    @@ -91,7 +91,7 @@ $('ul').prepend("<%=j render @list %>")
    ```
    [continue at 1:55 from first vid]
    [continue at 2:27 from first vid]
    http://flatiron-videos.s3.amazonaws.com/ruby-005/remote-true-ajax.mp4
    http://flatiron-videos.s3.amazonaws.com/ruby-005/low-level-remote-true.mp4
  8. zacksheppard revised this gist Jul 28, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions rails_ajax_forms.md
    Original file line number Diff line number Diff line change
    @@ -51,7 +51,6 @@ remote: :true is part of [rails/jquery-rails](https://github.com/rails/jquery-ra

    When you tell the form it is a remote form it adds what's known as the 'universal handler' which will select that form, bind an event to it's submit action, take off the original form envelope (the standard form submission), take the form data, put it in a new AJAX envelope, and send it out to the server using the original action.

    (Note: This example doesn't use a database so there would be a Where does the post go that isn't included here.)
    `views/index.erb.html` Create the list input form
    ```ruby
    # The 'remote: :true' here is the key that makes this work.
    @@ -68,11 +67,12 @@ When you tell the form it is a remote form it adds what's known as the 'universa
    * For this example the original action would be POST to the `def create` route with html and automatically get view/create.html.erb back.
    * It will now take the same POST to `def create` but since the request was in JS, it will look for view/create.js.erb instead.
    Convention over configuration!
    * If we were submitting a form for a nested resource (for a list task for instance) the form_for would instead look like this `<% form_for [@list, Task.new], remote: :true do |f| %>` Because task is a nested resource, and rails knows to look for a POST route of `list/1/task` we just need to pass it which list and rails will automatically use that route.

    `views/lists/create.js.erb`
    ```javascript
    // ?? Need to look up the j render. I assume it's javascript render?
    $('ul').prepend("<%=j render @list %>")
    // it renders the @list object passed to it. By convention it looks for a 'list' partial to render.
    ```

    `views/lists/_list.html.erb`
  9. zacksheppard revised this gist Jul 28, 2014. 1 changed file with 6 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion rails_ajax_forms.md
    Original file line number Diff line number Diff line change
    @@ -93,4 +93,9 @@ $('ul').prepend("<%=j render @list %>")
    [continue at 1:55 from first vid]
    http://flatiron-videos.s3.amazonaws.com/ruby-005/remote-true-ajax.mp4
    http://flatiron-videos.s3.amazonaws.com/ruby-005/low-level-remote-true.mp4
    http://flatiron-videos.s3.amazonaws.com/ruby-005/low-level-remote-true.mp4
    resources
    http://pcrglennon.github.io/blog/2014/07/20/starting-a-new-sinatra-app-from-scratch/
    http://guides.rubyonrails.org/working_with_javascript_in_rails.html
  10. zacksheppard revised this gist Jul 28, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion rails_ajax_forms.md
    Original file line number Diff line number Diff line change
    @@ -91,6 +91,6 @@ $('ul').prepend("<%=j render @list %>")
    ```
    [continue at 1:33 from first vid]
    [continue at 1:55 from first vid]
    http://flatiron-videos.s3.amazonaws.com/ruby-005/remote-true-ajax.mp4
    http://flatiron-videos.s3.amazonaws.com/ruby-005/low-level-remote-true.mp4
  11. zacksheppard revised this gist Jul 28, 2014. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions rails_ajax_forms.md
    Original file line number Diff line number Diff line change
    @@ -65,8 +65,10 @@ When you tell the form it is a remote form it adds what's known as the 'universa
    </ul>
    ```

    For this example the original action would be POST to the `def create` route with html and automatically get view/create.html.erb back.
    It will now take the same POST to `def create` but since the request was in JS, it will look for view/create.js.erb instead. Convention over configuration!
    * For this example the original action would be POST to the `def create` route with html and automatically get view/create.html.erb back.
    * It will now take the same POST to `def create` but since the request was in JS, it will look for view/create.js.erb instead.
    Convention over configuration!

    `views/lists/create.js.erb`
    ```javascript
    // ?? Need to look up the j render. I assume it's javascript render?
  12. zacksheppard revised this gist Jul 28, 2014. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion rails_ajax_forms.md
    Original file line number Diff line number Diff line change
    @@ -49,7 +49,7 @@ $(function(){
    This uses a built in rails method called remote to let the form know it will be using a remote submission that isn't the norm.
    remote: :true is part of [rails/jquery-rails](https://github.com/rails/jquery-rails)

    When you tell the form it is a remote form it adds what's known as the 'universal handler' which will select that form, bind an event to it's submit action, take off the original form envelope (the standard form submission), take the form data, put it in a new AJAX envelope, and send it out to the server.
    When you tell the form it is a remote form it adds what's known as the 'universal handler' which will select that form, bind an event to it's submit action, take off the original form envelope (the standard form submission), take the form data, put it in a new AJAX envelope, and send it out to the server using the original action.

    (Note: This example doesn't use a database so there would be a Where does the post go that isn't included here.)
    `views/index.erb.html` Create the list input form
    @@ -65,6 +65,8 @@ When you tell the form it is a remote form it adds what's known as the 'universa
    </ul>
    ```

    For this example the original action would be POST to the `def create` route with html and automatically get view/create.html.erb back.
    It will now take the same POST to `def create` but since the request was in JS, it will look for view/create.js.erb instead. Convention over configuration!
    `views/lists/create.js.erb`
    ```javascript
    // ?? Need to look up the j render. I assume it's javascript render?
  13. zacksheppard revised this gist Jul 28, 2014. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion rails_ajax_forms.md
    Original file line number Diff line number Diff line change
    @@ -87,4 +87,6 @@ $('ul').prepend("<%=j render @list %>")
    ```
    [continue at 1:33 from first vid]
    [continue at 1:33 from first vid]
    http://flatiron-videos.s3.amazonaws.com/ruby-005/remote-true-ajax.mp4
    http://flatiron-videos.s3.amazonaws.com/ruby-005/low-level-remote-true.mp4
  14. zacksheppard revised this gist Jul 28, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion rails_ajax_forms.md
    Original file line number Diff line number Diff line change
    @@ -50,7 +50,7 @@ This uses a built in rails method called remote to let the form know it will be
    remote: :true is part of [rails/jquery-rails](https://github.com/rails/jquery-rails)

    When you tell the form it is a remote form it adds what's known as the 'universal handler' which will select that form, bind an event to it's submit action, take off the original form envelope (the standard form submission), take the form data, put it in a new AJAX envelope, and send it out to the server.
    ----------------------------

    (Note: This example doesn't use a database so there would be a Where does the post go that isn't included here.)
    `views/index.erb.html` Create the list input form
    ```ruby
  15. zacksheppard revised this gist Jul 28, 2014. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions rails_ajax_forms.md
    Original file line number Diff line number Diff line change
    @@ -50,9 +50,8 @@ This uses a built in rails method called remote to let the form know it will be
    remote: :true is part of [rails/jquery-rails](https://github.com/rails/jquery-rails)

    When you tell the form it is a remote form it adds what's known as the 'universal handler' which will select that form, bind an event to it's submit action, take off the original form envelope (the standard form submission), take the form data, put it in a new AJAX envelope, and send it out to the server.

    (Note: This example doesn't use a database so there would be a Where does the post go that isn't included here.)
    ----------------------------
    (Note: This example doesn't use a database so there would be a Where does the post go that isn't included here.)
    `views/index.erb.html` Create the list input form
    ```ruby
    # The 'remote: :true' here is the key that makes this work.
  16. zacksheppard revised this gist Jul 28, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion rails_ajax_forms.md
    Original file line number Diff line number Diff line change
    @@ -51,7 +51,7 @@ remote: :true is part of [rails/jquery-rails](https://github.com/rails/jquery-ra

    When you tell the form it is a remote form it adds what's known as the 'universal handler' which will select that form, bind an event to it's submit action, take off the original form envelope (the standard form submission), take the form data, put it in a new AJAX envelope, and send it out to the server.

    (__Note:__ This example doesn't use a database so there would be a Where does the post go that isn't included here.
    (Note: This example doesn't use a database so there would be a Where does the post go that isn't included here.)
    ----------------------------
    `views/index.erb.html` Create the list input form
    ```ruby
  17. zacksheppard revised this gist Jul 28, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion rails_ajax_forms.md
    Original file line number Diff line number Diff line change
    @@ -51,7 +51,7 @@ remote: :true is part of [rails/jquery-rails](https://github.com/rails/jquery-ra

    When you tell the form it is a remote form it adds what's known as the 'universal handler' which will select that form, bind an event to it's submit action, take off the original form envelope (the standard form submission), take the form data, put it in a new AJAX envelope, and send it out to the server.

    __Note:__ This example doesn't use a database so there would be a Where does the post go that isn't included here.
    (__Note:__ This example doesn't use a database so there would be a Where does the post go that isn't included here.
    ----------------------------
    `views/index.erb.html` Create the list input form
    ```ruby
  18. zacksheppard revised this gist Jul 28, 2014. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions rails_ajax_forms.md
    Original file line number Diff line number Diff line change
    @@ -45,6 +45,7 @@ $(function(){
    ```
    ### AJAX form using `remote: :true`
    This uses a built in rails method called remote to let the form know it will be using a remote submission that isn't the norm.
    remote: :true is part of [rails/jquery-rails](https://github.com/rails/jquery-rails)

  19. zacksheppard revised this gist Jul 28, 2014. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions rails_ajax_forms.md
    Original file line number Diff line number Diff line change
    @@ -5,6 +5,7 @@ We will use a Todo list as an example domain for the examples.
    ### Lowest level of abstraction
    ---------------------------------
    `views/lists/index.erb.html` This will create the list input form
    (__Note__: This should also work in Sinatra)
    ```ruby
    <% form_for List.new do |f| %>
    <input id="new-todo" placeholder="What needs to be done?" autofocus="" autocomplete="off">
  20. zacksheppard revised this gist Jul 28, 2014. 1 changed file with 0 additions and 3 deletions.
    3 changes: 0 additions & 3 deletions rails_ajax_forms.md
    Original file line number Diff line number Diff line change
    @@ -3,9 +3,6 @@ We will use a Todo list as an example domain for the examples.


    ### Lowest level of abstraction


    (Note: This should also work in Sinatra)
    ---------------------------------
    `views/lists/index.erb.html` This will create the list input form
    ```ruby
  21. zacksheppard revised this gist Jul 28, 2014. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion rails_ajax_forms.md
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,8 @@ We will use a Todo list as an example domain for the examples.

    ### Lowest level of abstraction

    (Note: This should also work in Sinatra)

    (Note: This should also work in Sinatra)
    ---------------------------------
    `views/lists/index.erb.html` This will create the list input form
    ```ruby
  22. zacksheppard revised this gist Jul 28, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion rails_ajax_forms.md
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@ We will use a Todo list as an example domain for the examples.

    ### Lowest level of abstraction

    (__Note__: This should also work in Sinatra)
    (Note: This should also work in Sinatra)
    ---------------------------------
    `views/lists/index.erb.html` This will create the list input form
    ```ruby
  23. zacksheppard revised this gist Jul 28, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion rails_ajax_forms.md
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@ We will use a Todo list as an example domain for the examples.

    ### Lowest level of abstraction

    __Note__: This should also work in Sinatra
    (__Note__: This should also work in Sinatra)
    ---------------------------------
    `views/lists/index.erb.html` This will create the list input form
    ```ruby
  24. zacksheppard revised this gist Jul 28, 2014. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions rails_ajax_forms.md
    Original file line number Diff line number Diff line change
    @@ -3,6 +3,7 @@ We will use a Todo list as an example domain for the examples.


    ### Lowest level of abstraction

    __Note__: This should also work in Sinatra
    ---------------------------------
    `views/lists/index.erb.html` This will create the list input form
  25. zacksheppard revised this gist Jul 28, 2014. 1 changed file with 48 additions and 5 deletions.
    53 changes: 48 additions & 5 deletions rails_ajax_forms.md
    Original file line number Diff line number Diff line change
    @@ -5,9 +5,8 @@ We will use a Todo list as an example domain for the examples.
    ### Lowest level of abstraction
    __Note__: This should also work in Sinatra
    ---------------------------------
    `views/index.erb.html` This will create the list input form
    `views/lists/index.erb.html` This will create the list input form
    ```ruby

    <% form_for List.new do |f| %>
    <input id="new-todo" placeholder="What needs to be done?" autofocus="" autocomplete="off">
    <% end %>
    @@ -35,13 +34,57 @@ function createList(event){
    // prepend that data fomratted into a new li
    new_list_html = '<li><a href="lists/'+ json.id +'">'+ json.name +'</li>
    $('ul').prepend(new_list_html)
    });
    };
    // Event listener that will call the above function.
    $(function(){
    $("form.new_list").submit(createList);
    })
    ```
    ```
    ### AJAX form using `remote: :true`
    This uses a built in rails method called remote to let the form know it will be using a remote submission that isn't the norm.
    remote: :true is part of [rails/jquery-rails](https://github.com/rails/jquery-rails)

    When you tell the form it is a remote form it adds what's known as the 'universal handler' which will select that form, bind an event to it's submit action, take off the original form envelope (the standard form submission), take the form data, put it in a new AJAX envelope, and send it out to the server.

    __Note:__ This example doesn't use a database so there would be a Where does the post go that isn't included here.
    ----------------------------
    `views/index.erb.html` Create the list input form
    ```ruby
    # The 'remote: :true' here is the key that makes this work.
    <% form_for List.new, remote: :true do |f| %>
    <input id="new-todo" placeholder="What needs to be done?" autofocus="" autocomplete="off">
    <% end %>
    <ul id="todo-list">
    # The iterator '<% @lists.each do |list| %>' is now taken care of in `views/lists/create.js.erb`
    # The li itself is now in a partial in ``views/lists/_list.html.erb``
    </ul>
    ```

    `views/lists/create.js.erb`
    ```javascript
    // ?? Need to look up the j render. I assume it's javascript render?
    $('ul').prepend("<%=j render @list %>")
    ```

    `views/lists/_list.html.erb`
    ```ruby
    <li>
    <div class="view"><label><% link_to list.name, list %></label>
    </div>
    </li>
    ```

    `/assets/javascripts/lists.js` Notice that this file isn't here at all in this pattern
    ```javascript
    // The string injection is handled in the create.js.erb file
    // The event handler has been abstracted away in remote: :true
    // the post has also been abstracted away in remote: :true. When using remote: :true a new attribute to the form 'data-remote="true"'
    ```
    [continue at 1:33 from first vid]
  26. zacksheppard revised this gist Jul 28, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion rails_ajax_forms.md
    Original file line number Diff line number Diff line change
    @@ -22,7 +22,7 @@ __Note__: This should also work in Sinatra
    </ul>
    ```

    --------------------------------------
    ---------------------------------
    `/assets/javascripts/lists.js` This will listen to the form, catch the request, send it to the create route and reply modifying the page.
    ```javascript
    // create a function to handle the form data
  27. zacksheppard revised this gist Jul 28, 2014. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion rails_ajax_forms.md
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@ We will use a Todo list as an example domain for the examples.

    ### Lowest level of abstraction
    __Note__: This should also work in Sinatra

    ---------------------------------
    `views/index.erb.html` This will create the list input form
    ```ruby

    @@ -22,6 +22,7 @@ __Note__: This should also work in Sinatra
    </ul>
    ```

    --------------------------------------
    `/assets/javascripts/lists.js` This will listen to the form, catch the request, send it to the create route and reply modifying the page.
    ```javascript
    // create a function to handle the form data
  28. zacksheppard revised this gist Jul 28, 2014. 1 changed file with 5 additions and 4 deletions.
    9 changes: 5 additions & 4 deletions rails_ajax_forms.md
    Original file line number Diff line number Diff line change
    @@ -3,9 +3,11 @@ We will use a Todo list as an example domain for the examples.


    ### Lowest level of abstraction
    _Note_: This would also work in Sinatra
    __Note__: This should also work in Sinatra

    `views/index.erb.html` This will create the list input form
    ```ruby
    ## views/index.erb.html Create list input form

    <% form_for List.new do |f| %>
    <input id="new-todo" placeholder="What needs to be done?" autofocus="" autocomplete="off">
    <% end %>
    @@ -20,9 +22,8 @@ _Note_: This would also work in Sinatra
    </ul>
    ```

    `/assets/javascripts/lists.js` This will listen to the form, catch the request, send it to the create route and reply modifying the page.
    ```javascript
    // /assets/javascripts/lists.js Listen to the form, catch the request, send it to the create route and reply modifying the page.

    // create a function to handle the form data
    function createList(event){
    // prevent the form from submitting a normal http POST
  29. zacksheppard revised this gist Jul 28, 2014. 1 changed file with 25 additions and 1 deletion.
    26 changes: 25 additions & 1 deletion rails_ajax_forms.md
    Original file line number Diff line number Diff line change
    @@ -3,8 +3,9 @@ We will use a Todo list as an example domain for the examples.


    ### Lowest level of abstraction
    _Note_: This would also work in Sinatra
    ```ruby
    ## views/index.erb.html Task list and input form
    ## views/index.erb.html Create list input form
    <% form_for List.new do |f| %>
    <input id="new-todo" placeholder="What needs to be done?" autofocus="" autocomplete="off">
    <% end %>
    @@ -17,5 +18,28 @@ We will use a Todo list as an example domain for the examples.
    </li>
    <% end %>
    </ul>
    ```

    ```javascript
    // /assets/javascripts/lists.js Listen to the form, catch the request, send it to the create route and reply modifying the page.

    // create a function to handle the form data
    function createList(event){
    // prevent the form from submitting a normal http POST
    event.preventDefault();

    // AJAX post, using the forms specified "action". serialize() makes it a string. then JSON makes it JSON, which is a hash.
    $.post($this).attr("action"),$(this).serialize(), function(json){
    // prepend that data fomratted into a new li
    new_list_html = '<li><a href="lists/'+ json.id +'">'+ json.name +'</li>
    $('ul').prepend(new_list_html)
    });
    };
    $(function(){
    $("form.new_list").submit(createList);
    })
    ```
  30. zacksheppard created this gist Jul 28, 2014.
    21 changes: 21 additions & 0 deletions rails_ajax_forms.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    Notes from Flatiron School lecture w08.d04 showing different ways to us AJAX forms in Rails.
    We will use a Todo list as an example domain for the examples.


    ### Lowest level of abstraction
    ```ruby
    ## views/index.erb.html Task list and input form
    <% form_for List.new do |f| %>
    <input id="new-todo" placeholder="What needs to be done?" autofocus="" autocomplete="off">
    <% end %>
    <ul id="todo-list">
    <% @lists.each do |list| %>
    <li>
    <div class="view"><label><% link_to list.name, list %></label>
    </div>
    </li>
    <% end %>
    </ul>

    ```