Search code examples
javascriptruby-on-railsdeviseerbmaterialize

Clean approach to devise notice/alerts in materialize toast


I'm using devise in my rails app and as standard it comes with notice and alert methods which render on specific actions (e.g. a user signing in).

I am also using the Materialize CSS framework and they provide a nice clean 'Toast' style notification. This is the first approach at making notice and alert work with Toast.

<% if notice %>
  <script type="text/javascript">
    Materialize.toast('<%= notice %>', 4000)
  </script>
<% end %>
<% if alert %>
  <script type="text/javascript">
    Materialize.toast('<%= alert %>', 4000)
  </script>
<% end %>

Can anyone provide a cleaner/more DRY solution? Feels a little hacky.


Solution

  • It mast be not the "hackyest" way, but still a bit DRYer:

    <% flash.each do |message_type, message| %>
        <%= javascript_tag "Materialize.toast('#{message}', 4000)" %>
    <% end %>