Search code examples
javascriptcssruby-on-railstoasttoastr

rails-toastr: add a close button


I'm using the rails-toastr gem (Rails 5.0.0.1) and I want to add a close button to the toasters.

I followed the instructions and added links to toastr.css and toastr.js in application.html.erb. Then I created the two files under app/assets/stylesheets and app/assets/javascripts respectively and in the latter I added the line:

toastr.options.closeButton = true;

but the toasters don't come up.

I have this method in application_helper.rb (which I call in application.html.erb):

def custom_bootstrap_flash
  flash_messages = []
  flash.each do |type, message|
    type = 'success' if type == 'notice'
    type = 'error'   if type == 'alert'
    text = "<script>toastr.#{type}('#{message}');</script>"
    flash_messages << text.html_safe if message
  end
  flash_messages.join("\n").html_safe
end

but without the two assets, this works fine (but no close button of course).

Any ideas?


Solution

  • Thanks to this Upwork freelancer, we went for a solution similar to Beengie's. We added a toastr_override.js file in app/assets/javascripts/helpers/ with the following:

    //= require toastr/toastr
    toastr.options = Object.assign({}, toastr.options, {
        closeButton: true
    });