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 4118360
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T22:57:02+00:00 2026-05-20T22:57:02+00:00

I render an alert bar as a partial at the top of the screen

  • 0

I render an alert bar as a partial at the top of the screen that gets shown to the user for success/failure/notice flash messages.

I finally have it working for most scenarios, but the code itself is repetitive for a few parts and I am not sure how I can split it up more efficiently since I am relatively new to all of this. My goal is to try not to repeat myself if possible or at least to minimize how much is repeated.

For instance, is there a way to place some of the javascript into a re-useable partial or helper function? Are there other obvious ways of making this code less repetitious?

I’m not comfortable enough yet with Rails/Ruby to understand how to improve the code, so any tips you can provide are greatly appreciated!

/ top alert area
#topAlertBar.shadow_medium.soft-hidden

- if flash.empty? && !current_user.confirmed?
  - # User has yet to confirm their account
  - # and there AREN'T any flash messages to show

  #alertBarOffset.colordark.soft-hidden
    / placeholder for alert bar offset

  :javascript
    // Set the flash box content
    $('#topAlertBar').html('Please confirm your account by following the instructions sent to #{current_user.email}.  To resend your confirmation email, #{escape_javascript(link_to("click here", user_resend_confirmation_path(current_user), :class => "inlinelink", :method => :post, :remote => true))} #{escape_javascript(image_tag("ajaxOrange.gif", :class => "soft-hidden mls mbs"))}.');

    // Slides down the top alert bar after page load
    $('#topAlertBar, #alertBarOffset').delay(250).slideDown("fast");

    // Shows & hides AJAX loading GIF when necessary
    $('#topAlertBar a').click(function() {
      $(document).bind('ajaxSend', function(e, request, options) {
        $("#topAlertBar img").show();
      });
      $(document).bind('ajaxComplete', function(e, request, options) {
        $(document).unbind('ajaxSend', 'ajaxComplete');
        $("#topAlertBar img").hide();
      });
    });

- elsif !flash.empty? && !current_user.confirmed?
  - # User has yet to confirm their account
  - # and there ARE flash messages to show

  #alertBarOffset.colordark.soft-hidden
    / placeholder for alert bar offset

  - [:error, :success, :notice].each do |key| 
    - unless flash[key].blank?
      - @msg = flash[key]
      - @key = key

  :javascript
    // Set the flash box content
    var $that = $('#topAlertBar');
    $that.html('#{@msg}').addClass('#{@key}').delay(250).slideDown("fast", function() {
      $(this).delay(2000).slideUp("fast", function () {
        // Remove any CSS modifiers
        $that.removeClass('#{@key}');

        // Set the flash box content
        $('#topAlertBar').html('Please confirm your account by following the instructions sent to #{current_user.email}.  To resend your confirmation email, #{escape_javascript(link_to("click here", user_resend_confirmation_path(current_user), :class => "inlinelink", :method => :post, :remote => true))} #{escape_javascript(image_tag("ajaxOrange.gif", :class => "soft-hidden mls mbs"))}.');

        // Slides down the top alert bar after page load
        $('#topAlertBar, #alertBarOffset').slideDown("fast");

        // Shows & hides AJAX loading GIF when necessary
        $('#topAlertBar a').click(function() {
          $(document).bind('ajaxSend', function(e, request, options) {
            $("#topAlertBar img").show();
          });
          $(document).bind('ajaxComplete', function(e, request, options) {
            $(document).unbind('ajaxSend', 'ajaxComplete');
            $("#topAlertBar img").hide();
          });
        });

      });
    });


- elsif !flash.empty?
  - # User is confirmed
  - # and there ARE flash messages to show

  - [:error, :success, :notice].each do |key| 
    - unless flash[key].blank?
      - @msg = flash[key]
      - @key = key

  :javascript
    // Set the flash box content
    var $that = $('#topAlertBar');
    $that.html('#{@msg}').addClass('#{@key}').delay(250).slideDown("fast", function() {
      $(this).delay(2000).slideUp("fast");
    });
  • 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-20T22:57:03+00:00Added an answer on May 20, 2026 at 10:57 pm

    I ended up taking a different approach than what Jesse recommended, but he still helped get me thinking about ways to refactor the code. Here is the end result which is as DRY as I could get it without completely changing how I already had it implemented.

    Hopefully this will help someone else who stumbles upon this question in the future.


    In my ApplicationHelper (this is modified somewhat from the original question so it now works for my validation errors as well as regular flash messages)

      def display_flash_messages
        if !flash.empty?
          [:error, :success, :notice, :warning].each do |key| 
            unless flash[key].blank?
              @flash_key = key
              if flash[key].kind_of?(Array) && flash[key].size > 1
                @flash_msg = flash[key].join(' & ')
              elsif flash[key].kind_of?(Array) && flash[key].size == 1
                @flash_msg = flash[key].first
              elsif flash[key].kind_of?(String)
                @flash_msg = flash[key]
              end
            end
          end
        end
        return
      end
    

    In my main layout file, I’m just doing:

      %body
        - if signed_in?
          = render 'shared/top_alert_bar'
    

    In the top alert bar file

    = display_flash_messages
    
    / top alert area
    #topAlertBar.shadow_medium.soft-hidden
    - if !current_user.confirmed?
      #alertBarOffset.colordark.soft-hidden
        / placeholder for alert bar offset
    
    - if flash.empty? && !current_user.confirmed?
      - # User has yet to confirm their account
      - # and there AREN'T any flash messages to show
    
      :javascript
        #{render('shared/js/confirm_user')}
    
    - elsif !flash.empty?
    
      :javascript
        // Set the flash box content
        var $that = $('#topAlertBar');
        $that.html('#{@flash_msg}').addClass('#{@flash_key}').delay(250).slideDown("fast", function() {
          $(this).delay(4000).slideUp("fast", function () {
            // Remove any CSS modifiers
            $that.removeClass('#{@flash_key}');
    
            #{!current_user.confirmed? ? render('shared/js/confirm_user') : ""}
    
          });
        });
    

    In the confirm_user partial

    :plain
      $('#topAlertBar').html('Please confirm your account by following the instructions sent to #{current_user.email}.  To resend your confirmation email, #{escape_javascript(link_to('click here', user_resend_confirmation_path(current_user), :class => 'inlinelink', :method => :post, :remote => true))}. #{escape_javascript(image_tag('ajaxOrange.gif', :class => 'soft-hidden mls mbs'))}');
    
      $('#topAlertBar, #alertBarOffset').delay(250).slideDown('fast');
    

    And finally, I moved this to my main js file

    /* ******************************** */
    /* Top Alert Bar for Flash Messages */
    /* ******************************** */
    // Description: Shows & hides AJAX loading GIF when necessary
    $('#topAlertBar a').click(function() {
      $(document).bind('ajaxSend', function(e, request, options) {
        $("#topAlertBar img").show();
      });
      $(document).bind('ajaxComplete', function(e, request, options) {
        $("#topAlertBar img").hide();
        $(document).unbind('ajaxSend', 'ajaxComplete');
      });
    

    });

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

Sidebar

Related Questions

I use RenderAction to render a partial that is used all over my site.
How to render partial view in some specific div without using ajax in asp.net
I want to render the partial view using ajax. When I submit the button,
I have the following in my controller: return render :js => alert('Hello Rails'); Problem
I want to render a view when user clicks on Add New button on
I'm trying to render a custom flash message from a Devise controller. It is
app.get('/',function(req,res){ res.render('home'); // I want the template to be able to access the flash
In my Rails 3 application I do: render :js => alert(\Error!\\nEmpty message sent.\); if
I wrote the following code to display the show.html.erb view with an alert flash
I'm trying to simply render a flash message when a form is improperly submitted

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.