Skip to content

Instantly share code, notes, and snippets.

@MyklClason
Last active November 7, 2017 01:48
Show Gist options
  • Save MyklClason/4d0fdeb27572f2aa40f8d0973844eb57 to your computer and use it in GitHub Desktop.
Save MyklClason/4d0fdeb27572f2aa40f8d0973844eb57 to your computer and use it in GitHub Desktop.
Simple way to handle Bootstrap flash messages when using AJAX and the twitter-bootstrap-rails gem.
// The below must be included somewhere in in .js.erb file.
...
$('#bootstrap-flash').html("<%= j (bootstrap_flash) %>");
...
Before:
...
<div class="col-lg-12">=
<%= bootstrap_flash %>
<%= yield %>
</div>
...
After:
...
<div class="col-lg-12">
<div id="bootstrap-flash">
<%= bootstrap_flash %>
</div>
<%= yield %>
</div>
...
@ppyxmw
Copy link

ppyxmw commented Nov 7, 2017

Just to add, in the controller action (e.g. destroy) for an address model, you need to add ".now" after flash so that the notification does not repair when you refresh the page. See below:

def destroy
@address = Address.destroy(params[:id])
respond_to do |wants|
wants.html { redirect_to addresses_path }
wants.js { flash.now[:danger] = "Address deleted." }
end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment