Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4028208
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:11:32+00:00 2026-05-20T11:11:32+00:00

I’ve been trying to make the styling added to this flash message in my

  • 0

I’ve been trying to make the styling added to this flash message in my application layout work as desired:

<div id= "notice"><% flash.each do |key, value| %>
   <%= value %>
<% end %></div>

Here’s the CSS:

#notice {
    background: #e6efc2;
    color: #264409;
    border-color: #c6d880;
    padding: 0.8em;
    margin-bottom: 1em;
    border: 2px solid #ddd;
}     

Now the problem with this is that the flash message is rendered with ajax, and so the styling is supposed to appear only when the flash message appears. However, this is not the case. A green div appears even when there is no flash message, and this looks rather awkward.

Now I’ve tried something like this:

<div id= "notice"><% flash.each do |key, value| %>
  <div class="<%= key %>"><%= value %></div>
<% end %></div>

where I use the "<%= key%>" class for styling. I leave the <div id="notice"> on the outside for the ajax rendering because it doesn’t seem to pick up the div within the ruby block. However, the CSS styling does not get rendered with the "<%= key%>" class. So it seems like i’m stuck between two bad options:

  1. Have an awkward green div in my app at all times that only looks how it should when the flash message is rendered or
  2. Not have the awkward green div, but then have no styling for the flash message so that it just looks like some awkward text.

Is there any way that I can get the best of both worlds?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T11:11:32+00:00Added an answer on May 20, 2026 at 11:11 am

    Can you share your AJAX that is generating this?

    One thing I would try in your situation is using jQuery’s append() method to add the flash DIV to the top of the page when the flash message is triggered, and then take it away after some timer.

    –EDIT–

    Here’s a “for instance” example:

    Let’s just say your ajax call is hitting FoosController#Update, and is sent from the #show action.

    In the show view I would have something like:

    <div class="notices"></div>
    

    I would put your flash messages in a partial (makes the ajax code easier), so something like
    views/shared/_flash.html.erb…

    <div class="flash"><% flash.each do |key, value| %>
       <%= value %>
    <% end %></div>
    

    In jQuery + ERB I would write an update.js.erb file in the views/foos/

    $('div.notices').append('<%= render 'shared/flash' %>');
    

    Then to make it automatically fade out you could include a script (either in the specific view with flash messages or in the application.js file) like:

    $(document).ready(function() {
      $('div.flash').delay(5000).fadeOut();
    });
    

    That would make any div.flash fade out and disappear after 5 seconds. Alternatively you could make it go away after click using the jQuery click() method.

    I hope that helps.

    –EDIT AGAIN–

    According to this railscast, to do this with RJS:

    In your view create:

    <div id="notices"></div>
    

    In your RJS file do:

    page.insert_html :bottom, :partial=>'shared/flash'
    

    It’s still a good idea to put your flash messages in a partial. Be sure and put the flash messages in div.flash as described in the first edit, and you should be good to go.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6
I have a bunch of posts stored in text files formatted in yaml/textile (from
We're building an app, our first using Rails 3, and we're having to build
I'm making a simple page using Google Maps API 3. My first. One marker

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.