(Этот вопрос является продолжением следующей темы:
У меня есть форма:
<%= 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 %>
У меня есть кнопка отправки и флажок рядом с ней:
<%= 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 %>
Когда пользователь устанавливает флажок и нажимает кнопку Generate Report
, я хотел бы отобразить предупреждение для пользователя «Ваш отчет будет отправлен вам по электронной почте через несколько минут»
Как мне этого достичь:
- Должно отображаться флэш-сообщение
- Перенаправление страниц или рендеринг не должны происходить
Пожалуйста, помогите!
UPDATE:
- Я добавил это в свой контроллер (называемый
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>
- И это мой код для добавления кнопки и флажка в моей форме:
<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>
С приведенным выше кодом, флэш-сообщение все еще не появляется для меня!
ОБНОВЛЕНИЕ: (обновление2)
Я попытался изменить flash.now[:alert]
на flash[:alert]
, но все равно флэш-сообщение не появляется
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