Last active
November 7, 2017 01:48
-
-
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.
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
// The below must be included somewhere in in .js.erb file. | |
... | |
$('#bootstrap-flash').html("<%= j (bootstrap_flash) %>"); | |
... |
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
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> | |
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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