0

(This question is a continuation of the following thread:

I have a form:

<%= form_tag generate_report_path(:header => true) do |f| % >
<div class="container-fluid">
  <div style="padding-right:10px">

    <%= select_tag(:report_id, options_for_select(
      [["Select Report Type", 0],
      ["Report1", 1],
      ["Report2", 2],
      ["Report3", 3]]), id: "report_selection") %>


      <%= hidden_field_tag :format, :pdf %>

I have a submit button and a checkbox right next to it:

<%= button_tag "Generate Report", class: 'btn btn-sm btn-primary'%>
<%= label_tag do %>
  <%= check_box_tag "format_reqd", "format_reqd", false %>
  Check for Numeric format 
<% end %>

When the user selects the checkbox and clicks on the Generate Report button, I would like to display an alert to the user "your report would be emailed to you in a few minutes"

How can I achieve these:

  1. A flash message should be displayed
  2. Page redirection or rendering should not happen

Please help!

UPDATE:

  1. I added this to my controller (called report_controller):
    def reportgen

            respond_to do |format|
              format.json { flash.now[:alert] = "Report generation has been initiated. You will receive an email in approximately 4 - 5 minutes with a link to download the report."}
            end

    end
  1. \app\views\report\reportgen.js.erb
    $("#flash").html('<%= j render partial: "shared/notice_banner" %>');
  1. application.html.erb
  <div id="flash">
    <% if alert.present? %>
      <%= render partial: "shared/notice_banner" %>
    <% end %>
  </div>

  <% flash.each do |key, value| %>
    <% if key.to_s == "alert" %>
      <div style= "color: #FF0000" class="text-center <%= flash_class(key) %>">
      <%= value %>
    </div>
    <% else %>
      <div class="lead text-center <%= flash_class(key) %>">
      <%= value %>
    </div>
    <% end %>    
  <% end %>



  <%= yield %>
  1. app\views\shared\_notice_banner.html.erb
<div data-alert class="alert-box">
  <%= alert %>
  <a href="#" class="close">&times;</a>
</div>
  1. And this is my code to add the button and checkbox in my form:
                  <div id="generate_button" style="display: none; float:left;">
                    <%= button_tag "Generate Report", class: 'btn btn-sm btn-primary'%>
                  </div>
                  &nbsp;
                  <div id="report_type_drop" style="display: none;padding-top:5px;padding-left:5px">


                    <%= label_tag do %>
                      <%= check_box_tag "format_reqd", "format_reqd", false %>
                      Check for Numeric format
                    <% end %>
                    </div>

With the above code, flash message still does not appear for me!

UPDATE: (update2)

I tried changing flash.now[:alert] to flash[:alert] but still the flash message does not appear

            respond_to do |format|
              format.json { flash[:alert] = "Report generation has been initiated. You will receive an email in approximately 4 - 5 minutes with a link to download the report."}
            end
m.beginner
  • 389
  • 2
  • 18

1 Answers1

0

You need put some javascript async actions in order to do that, you have multiple options:

  • Submit the form with ajax, then parse the response and show the alert using javascript
  • Use ajax to check if there is a change in the alert in the server every x seconds
  • Use web sockets to check in real time if there is a new alert

which one you want to implement depend of your specific needs

osdamv
  • 3,493
  • 2
  • 21
  • 33