Created
March 10, 2015 19:41
-
-
Save volontarian/09e7283750ea5fa3f8e3 to your computer and use it in GitHub Desktop.
Multi step form / wizard responses for Ajax (modals) and HTML requests.
This file contains 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
<% message = flash[:notice] || flash[:alert] %> | |
<% flash.delete(:notice); flash.delete(:alert) %> | |
<% alert = message.present? ? "alert('#{message}');" : '' %> | |
<% if <a href="/path">@path</a>.present? %> | |
$.ajax({ url: "<%= @path %>", data: <%= raw @data.to_json %>, type: "<%= @method.to_s.upcase %>", dataType: "script"}).done(function(data) { | |
eval(data); | |
<%= raw alert %> | |
}) | |
.fail(function(data) { | |
<%= raw alert %> | |
alert("Failed to load <%= @path %>!"); | |
}); | |
<% elsif @template.present? %> | |
<% if @template_format == 'html' %> | |
<% if @target_is_modal %> | |
$(@target).html("<%= escape_javascript( | |
render( | |
partial: 'shared/layouts/twitter_bootstrap/modal', | |
locals: { title: @modal_title, body: render(template: "#{controller_name}/#{@template}.html") } | |
) | |
) %>"); | |
<% else %> | |
$("<%= @target %>").html("<%= escape_javascript render(template: "#{controller_name}/#{@template}.html") %>"); | |
<% end %> | |
<% elsif @template_format == 'js' %> | |
<%= render template: "#{controller_name}/#{@template}.js" %> | |
<% end %> | |
<%= raw alert %> | |
<% elsif message.present? %> | |
<%= raw alert %> | |
<% end %> |
This file contains 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
<% body ||= nil %> | |
<% footer ||= nil %> | |
<div class="modal-header"> | |
<button type="button" id="close_bootstrap_modal_button" class="close" data-dismiss="modal" aria-hidden="true">×</button> | |
<h3><%= title %></h3> | |
</div> | |
<div class="modal-body" style="overflow-y:none;"> | |
<%= body || yield(:modal_body) %> | |
</div> | |
<div class="modal-footer"> | |
<%= footer || yield(:modal_footer) %> | |
</div> |
This file contains 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 ApplicationController < ActionController::Base | |
private | |
def render_or_redirect_by_request_type | |
if request.xhr? || request.env['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest' | |
render_modal_javascript_response | |
elsif @template.present? | |
render @template | |
elsif @path.present? | |
redirect_to @path | |
end | |
end | |
def render_javascript_response | |
@method ||= :get | |
@data ||= {} | |
@template ||= action_name unless <a href="/path">@path</a>.present? | |
@template_format ||= 'html' | |
@target ||= "#bootstrap_modal" | |
@target_is_modal = @target_is_modal.nil? ? true : @target_is_modal | |
@modal_title ||= I18n.t("#{controller_name}.#{action_name}.title") | |
render partial: 'shared/javascript_response.js', layout: false | |
end | |
end |
This file contains 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 YourRailsController < ApplicationController | |
def your_ajax_and_html_action | |
render_or_redirect_by_request_type | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment