Skip to content

Instantly share code, notes, and snippets.

@kstevens715
Created June 2, 2012 01:03
Show Gist options
  • Save kstevens715/2856013 to your computer and use it in GitHub Desktop.
Save kstevens715/2856013 to your computer and use it in GitHub Desktop.
CoffeeScript file for working with Twitter Bootstrap forms.
# This is just a basic sample of a controller needed to work with popups.js
class AddressesController < ApplicationController
before_filter :load_addressable
def new
@address = @addressable.addresses.build(:name => params[:name])
render :partial => 'form'
end
def create
@address = @addressable.addresses.build(params[:address])
if @address.save
flash[:notice] = "Successfully created address"
render :nothing => true, :status => :created
else
render :partial => 'form'
end
end
def edit
@address = @addressable.addresses.find_by_name(params[:name])
render :partial => 'form'
end
def update
@address = @addressable.addresses.find(params[:id])
if @address.update_attributes(params[:address])
flash[:notice] = "Successfully updated address"
render :nothing => true, :status => :created
else
render :partial => 'form'
end
end
private
def load_addressable
klass = [Client].detect { |c| params["#{c.name.underscore}_id"] }
@addressable = klass.find(params["#{klass.name.underscore}_id"])
end
end
jQuery ->
$('a.popup').click ->
$('#popup_container').load($(this).attr('href')).modal('show')
false
# Submit the popup form.
$('#popup_container').on("submit", "form", (event)->
event.preventDefault()
jqxhr = $.post(this.action, $(this).serialize(), (data, status, jqxhr)->
if jqxhr.status == 200 # validation error
$('#popup_container').html(data)
else if jqxhr.status == 201 # entity updated or created
$('#popup_container').modal('hide')
# Refresh page when popup closes to update any data that may have
# changed.
location.reload()
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment