Search code examples
ruby-on-railsflash-message

Flash message not showing in rails


i've some issue when trying to use flash message flash[:notice]. the flash message is not showing it's message

Here is my partial form view

<%= form_tag bulk_push_api_v1_notifications_path do |f| %>
<fieldset class="inputs">
    <legend>
        <span>Details</span>
    </legend>
        <% if flash[:notice].present? %>
          <p class='flash-notice'><%= flash[:notice] %></p>
        <% elsif flash[:error].present? %>
          <p class='flash-error'><%= flash[:error] %></p>
        <% end %>
    <ol>
        <li class="file input required" id="play_media_input">
             <%= label_tag(:message, "Message : ") %>
             <%= text_area_tag :message,  nil, :required => true %>
            <p class="inline-hints">Only text can be sent</p>
        </li>
    </ol>
</fieldset>
<fieldset class="actions">
    <ol>
        <li class="action input_action " id="play_submit_action">
            <%= submit_tag("Send Notification") %>
        </li>
    </ol>
</fieldset>

and it will triggered this method from controller

def bulk_push
  begin
    User.send_bulk_notifications(params[:message])
    redirect_to admin_notification_path, :flash => { :notice => "Insufficient rights!" }
  rescue
    redirect_to admin_notification_path, :flash => { :error => "Error" }
  end

end

Solution

  • Try below code to display flash messages:

    controller

    def bulk_push
      begin
        User.send_bulk_notifications(params[:message])
        redirect_to admin_notification_path, notice: "Insufficient rights!"
      rescue
        redirect_to admin_notification_path, alert: "Error"
      end
    
    end
    

    app/views/layouts/application.html.erb

    <% if notice %>
      <p class="alert alert-success"><%= notice %></p>
    <% end %>
    <% if alert %>
      <p class="alert alert-danger"><%= alert %></p>
    <% end %>
    
    <style type="text/css">
      .alert-success{
        color: green;
      }
      .alert-danger{
        color: red;
      }
    </style>