Last active
August 29, 2015 14:05
-
-
Save ericwoodruff/bbbe3e97f0b086d5c66c to your computer and use it in GitHub Desktop.
rails form_for data-update-target ajax
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // http://mrdanadams.com/2011/partials-ajax-form-submit-rails/#.UWcy9kDCHsg | |
| // http://stackoverflow.com/questions/18770517/rails-4-how-to-use-document-ready-with-turbo-links | |
| $(document).on ('ajax:success', 'form[data-update-target], a[data-update-target]', function (evt, data) { | |
| var dom = $(data); | |
| $('#messages').html(dom.siblings('#messages:first').contents()); | |
| var target = $(this).data('update-target'); | |
| $('#' + target).html(dom.siblings('#body:first').contents()); | |
| }); | |
| $(document).on ('ajax:error', 'form[data-update-target], a[data-update-target]', function (evt, data) { | |
| //TODO $('#messages').html(error.responseText); | |
| $('#messages').text("An error has occurred."); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| - flash.each do |name, msg| | |
| - if msg.is_a?(String) | |
| - msg = [msg] | |
| - if msg.is_a? Array and not msg.empty? | |
| %div{:class => "alert alert-#{name == :notice ? "success" : "error"}"} | |
| %a.close{"data-dismiss" => "alert"} × | |
| - msg.each do |msg| | |
| = content_tag :div, msg, :id => "flash_#{name}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #messages | |
| = render partial: 'shared/messages' | |
| #body | |
| = yield |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class HomeController < ApplicationController | |
| layout 'ajax', only: [:search] | |
| def search | |
| # ... do work | |
| render 'home/_search_results' | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| = form_for @query, url: { action: :search }, remote: true, html: { method: :get }, data: { update_target: 'results' } do |f| |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment