Skip to content

Instantly share code, notes, and snippets.

@calebhearth
Last active August 29, 2015 13:55
Show Gist options
  • Save calebhearth/8710014 to your computer and use it in GitHub Desktop.
Save calebhearth/8710014 to your computer and use it in GitHub Desktop.
class SectionsController < ApplicationController
def update
section = course.sections.find(params[:id])
section.update(section_params)
render section_json(section)
end
private
def section_json(section)
section_html = render_to_string section, location: nil, formats: [:html]
section_form_html = render_to_string 'sections/_form', locals: { section: section }, layout: false
{ json: { section_html: section_html, section_form_html: section_form_html } }
end
end
$ ->
$('#sections-list').on 'ajax:success', 'form.edit_section', Callbacks.reloadSectionAndForm
Callbacks =
reloadSectionAndForm: (e, json) ->
parentElement = $(e.delegateTarget)
Callbacks._replaceFromJson(parentElement, json.section_html)
Callbacks._replaceFromJson(parentElement, json.section_form_html)
Callbacks._resetToggles
_replaceFromJson: (parentElement, html) ->
element = $(html)
parentElement.find("##{element.attr('id')}").replaceWith(element)
_resetToggles: ->
$('[data-toggle-target]').each ->
$(@).toggleTarget()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment