(This question is a continuation of the following thread:
In Rails, is it possible to display an alert without calling redirect_to or render?
Continuing that question here since there were too many comments in the earlier thread
- There were 2 issues stated in that question and one of them was resolved in that thread
- Hence opening this thread to focus on only the other issue which was not resolved in that 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:
- A flash message should be displayed
- Page redirection or rendering should not happen
Please help!
UPDATE:
- 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
\app\views\report\reportgen.js.erb
$("#flash").html('<%= j render partial: "shared/notice_banner" %>');
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 %>
app\views\shared\_notice_banner.html.erb
<div data-alert class="alert-box">
<%= alert %>
<a href="#" class="close">×</a>
</div>
- 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>
<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