Clyde Brown
Clyde Brown

Reputation: 217

Ruby flash messages with javascript won't appear

I'm trying to use my flash messages with some javascript functionality, but I'm having trouble with displaying the messages upon user sign-in and sign out. What might the problem be?

This is in the application.html.erb of my rails app.

<!-- Flash Messages -->
<% if flash[:notice] %>
  <script type='text/javascript'>
    Messenger().post({
      message: flash[:notice],
    })
    </script>
<% elsif flash[:error] %>
  <script type='text/javascript'>
    Messenger().post({
      message: flash[:error],
    })
  </script>
<% else %>
<% end %>
<% if flash[:alert] %>
  <p class="alert alert-danger"><%= flash[:alert] %></p>
<% end %>
<!-- /Flash Messages -->

I'm using javascript files in my assets folder. Hopefully when this is all done the messages will appear on the bottom right instead of the default top of the page.

Upvotes: 0

Views: 41

Answers (1)

Gerry
Gerry

Reputation: 10507

You need to enclose between <%= and %> both flash[:notice] and flash[:error] messages:

<% if flash[:notice] %>
  <script type='text/javascript'>
    Messenger().post({
      message: <%= flash[:notice] %>
    })
  </script>
<% elsif flash[:error] %>
  <script type='text/javascript'>
    Messenger().post({
      message: <%= flash[:error] %>
    })
  </script>
<% else %>
<% end %>
<% if flash[:alert] %>
  <p class="alert alert-danger"><%= flash[:alert] %></p>
<% end %>

Upvotes: 2

Related Questions