Skip to content

Instantly share code, notes, and snippets.

@delba
Last active December 19, 2015 14:29
Show Gist options
  • Select an option

  • Save delba/5969191 to your computer and use it in GitHub Desktop.

Select an option

Save delba/5969191 to your computer and use it in GitHub Desktop.
client id
class Item < ActiveRecord::Base
end
$(document).on 'submit', '#new_item', (e) ->
e.preventDefault()
$.ajax
type: 'POST'
url: '/items'
dataType: 'json'
data: $(this).serialize()
beforeSend: (xhr) =>
xhr.cid = Date.now()
$(this).after "<article id='#{xhr.cid}'> #{item_name.value} </article>"
@reset()
success: (item, status, xhr) ->
$("##{xhr.cid}").replaceWith "<article id='#{item.id}'> #{item.name} </article>"
class ItemsController < ApplicationController
respond_to :json
def create
@item = Item.create(item_params)
respond_with @item
end
private
def item_params
params.require(:item).permit(:name)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment