在 Rails 中,是否可以在不调用 redirect_to 或 render 的情况下显示警报?
[英] In Rails, is it possible to display an alert without calling redirect_to or render?
本文介绍了在 Rails 中,是否可以在不调用 redirect_to 或 render 的情况下显示警报?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个表格:
<%= 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([[选择报告类型", 0],[报告1",1],[报告2",2],["Report3", 3]]), id: "report_selection") %><%= hidden_field_tag :format, :pdf %>
我旁边有一个提交按钮和一个复选框:
<%= button_tag "生成报告", class: 'btn btn-sm btn-primary'%><%= label_tag do %><%= check_box_tag "format_reqd", "format_reqd", false %>检查数字格式<%结束%>
当用户选中复选框并点击生成报告
按钮时,我想向用户显示您的报告将在几分钟内通过电子邮件发送给您"的提示;但不应刷新页面.
我怎样才能实现这两个:
- 禁用页面刷新?
- 显示快闪信息?
请帮忙!
更新:
- 我将此添加到我的控制器(称为
report_controller
):
def reportgenresponse_to do |格式|format.js { flash.now[:alert] = "报告生成已启动.您将在大约 4 - 5 分钟后收到一封电子邮件,其中包含下载报告的链接."}结尾结尾
\app\views\report\reportgen.js.erb
$("#flash").html('<%= j render partial: "shared/notice_banner" %>');
application.html.erb
<% 如果 alert.present?%><%=渲染部分:shared/notice_banner"%><%结束%>
<% flash.each do |key, value|%><% if key.to_s == "alert" %><div style="color: #FF0000" class="text-center <%= flash_class(key) %>"><%=值%>
<%其他%><div class="lead text-center <%= flash_class(key) %>"><%=值%>
<%结束%><%结束%><%=产率%>
app\views\shared\_notice_banner.html.erb
<%=警报%><a href="#" class="close">×</a>
- 这是我在表单中添加按钮和复选框的代码:
使用上面的代码,这些是我现在原始问题中 2 个问题的结果:
- 禁用页面刷新 - 现在可以正常工作
- 显示闪现信息 - 这不起作用!
更新:
我现在也看到了这个错误(不过不确定是什么原因造成的):
ActionController::UnknownFormat(ReportController#reportgen 缺少此请求格式和变体的模板.request.formats: ["application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"]request.variant: []):actionpack (5.0.7.1) lib/action_controller/metal/implicit_render.rb:43:in `default_render'actionpack (5.0.7.1) lib/action_controller/metal/basic_implicit_render.rb:4:in `block in send_action'actionpack (5.0.7.1) lib/action_controller/metal/basic_implicit_render.rb:4:in `tap'actionpack (5.0.7.1) lib/action_controller/metal/basic_implicit_render.rb:4:in `send_action'actionpack (5.0.7.1) lib/abstract_controller/base.rb:188:in `process_action'actionpack (5.0.7.1) lib/action_controller/metal/rendering.rb:30:in `process_action'actionpack (5.0.7.1) lib/abstract_controller/callbacks.rb:20:in `block in process_action'activesupport (5.0.7.1) lib/active_support/callbacks.rb:126:in `call'activesupport (5.0.7.1) lib/active_support/callbacks.rb:506:in `block (2 levels) in compile'activesupport (5.0.7.1) lib/active_support/callbacks.rb:455:in `call'activesupport (5.0.7.1) lib/active_support/callbacks.rb:448:in ‘block (2 levels) in around’activesupport (5.0.7.1) lib/active_support/callbacks.rb:286:in `block (2 levels) in pauses'审计 (4.8.0) lib/audited/sweeper.rb:14:in `around'activesupport (5.0.7.1) lib/active_support/callbacks.rb:405:in `public_send'activesupport (5.0.7.1) lib/active_support/callbacks.rb:405:in `block in make_lambda'activesupport (5.0.7.1) lib/active_support/callbacks.rb:285:in `block in暂停'activesupport (5.0.7.1) lib/active_support/callbacks.rb:447:in ‘block in around’activesupport (5.0.7.1) lib/active_support/callbacks.rb:455:in `call'activesupport (5.0.7.1) lib/active_support/callbacks.rb:448:in ‘block (2 levels) in around’activesupport (5.0.7.1) lib/active_support/callbacks.rb:286:in `block (2 levels) in pauses'审计 (4.8.0) lib/audited/sweeper.rb:14:in `around'activesupport (5.0.7.1) lib/active_support/callbacks.rb:405:in `public_send'activesupport (5.0.7.1) lib/active_support/callbacks.rb:405:in `block in make_lambda'activesupport (5.0.7.1) lib/active_support/callbacks.rb:285:in `block in暂停'activesupport (5.0.7.1) lib/active_support/callbacks.rb:447:in ‘block in around’activesupport (5.0.7.1) lib/active_support/callbacks.rb:455:in `call'activesupport (5.0.7.1) lib/active_support/callbacks.rb:101:in `__run_callbacks__'activesupport (5.0.7.1) lib/active_support/callbacks.rb:750:in `_run_process_action_callbacks'activesupport (5.0.7.1) lib/active_support/callbacks.rb:90:in `run_callbacks'actionpack (5.0.7.1) lib/abstract_controller/callbacks.rb:19:in `process_action'actionpack (5.0.7.1) lib/action_controller/metal/rescue.rb:20:in `process_action'actionpack (5.0.7.1) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'activesupport (5.0.7.1) lib/active_support/notifications.rb:164:in `block in instrument'activesupport (5.0.7.1) lib/active_support/notifications/instrumenter.rb:21:in `instrument'activesupport (5.0.7.1) lib/active_support/notifications.rb:164:in `instrument'actionpack (5.0.7.1) lib/action_controller/metal/instrumentation.rb:30:in `process_action'actionpack (5.0.7.1) lib/action_controller/metal/params_wrapper.rb:248:in `process_action'activerecord (5.0.7.1) lib/active_record/railties/controller_runtime.rb:18:in `process_action'actionpack (5.0.7.1) lib/abstract_controller/base.rb:126:in `process'actionview (5.0.7.1) lib/action_view/rendering.rb:30:in `process'actionpack (5.0.7.1) lib/action_controller/metal.rb:190:in `dispatch'actionpack (5.0.7.1) lib/action_controller/metal.rb:262:in `dispatch'actionpack (5.0.7.1) lib/action_dispatch/routing/route_set.rb:50:in `dispatch'actionpack (5.0.7.1) lib/action_dispatch/routing/route_set.rb:32:in `serve'actionpack (5.0.7.1) lib/action_dispatch/journey/router.rb:39:in `block in serve'actionpack (5.0.7.1) lib/action_dispatch/journey/router.rb:26:in `each'actionpack (5.0.7.1) lib/action_dispatch/journey/router.rb:26:in `serve'actionpack (5.0.7.1) lib/action_dispatch/routing/route_set.rb:727:in `call'newrelic_rpm (6.3.0.355) lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'omniauth (1.4.2) lib/omniauth/strategy.rb:186:in `call!'omniauth (1.4.2) lib/omniauth/strategy.rb:164:in `call'newrelic_rpm (6.3.0.355) lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'newrelic_rpm (6.3.0.355) lib/new_relic/rack/agent_hooks.rb:30:in `traced_call'newrelic_rpm (6.3.0.355) lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'newrelic_rpm (6.3.0.355) lib/new_relic/rack/browser_monitoring.rb:32:in `traced_call'newrelic_rpm (6.3.0.355) lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'rack (2.0.6) lib/rack/etag.rb:25:in `call'newrelic_rpm (6.3.0.355) lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'rack (2.0.6) lib/rack/conditional_get.rb:38:in `call'newrelic_rpm (6.3.0.355) lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'rack (2.0.6) lib/rack/head.rb:12:in `call'newrelic_rpm (6.3.0.355) lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'机架 (2.0.6) lib/rack/session/abstract/id.rb:232:in `context'rack (2.0.6) lib/rack/session/abstract/id.rb:226:in `call'newrelic_rpm (6.3.0.355) lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'actionpack (5.0.7.1) lib/action_dispatch/middleware/cookies.rb:613:in `call'newrelic_rpm (6.3.0.355) lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'activerecord (5.0.7.1) lib/active_record/migration.rb:553:in `call'newrelic_rpm (6.3.0.355) lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'actionpack (5.0.7.1) lib/action_dispatch/middleware/callbacks.rb:38:in `block in call'activesupport (5.0.7.1) lib/active_support/callbacks.rb:97:in `__run_callbacks__'activesupport (5.0.7.1) lib/active_support/callbacks.rb:750:in `_run_call_callbacks'activesupport (5.0.7.1) lib/active_support/callbacks.rb:90:in `run_callbacks'actionpack (5.0.7.1) lib/action_dispatch/middleware/callbacks.rb:36:in `call'newrelic_rpm (6.3.0.355) lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'actionpack (5.0.7.1) lib/action_dispatch/middleware/executor.rb:12:in `call'newrelic_rpm (6.3.0.355) lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'actionpack (5.0.7.1) lib/action_dispatch/middleware/remote_ip.rb:79:in `call'newrelic_rpm (6.3.0.355) lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'actionpack (5.0.7.1) lib/action_dispatch/middleware/debug_exceptions.rb:49:in `call'newrelic_rpm (6.3.0.355) lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'web-console (3.7.0) lib/web_console/middleware.rb:135:in `call_app'web-console (3.7.0) lib/web_console/middleware.rb:22:in `block in call'web-console (3.7.0) lib/web_console/middleware.rb:20:in `catch'web-console (3.7.0) lib/web_console/middleware.rb:20:in `call'newrelic_rpm (6.3.0.355) lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'actionpack (5.0.7.1) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'newrelic_rpm (6.3.0.355) lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'railties (5.0.7.1) lib/rails/rack/logger.rb:36:in `call_app'railties (5.0.7.1) lib/rails/rack/logger.rb:24:in `block in call'activesupport (5.0.7.1) lib/active_support/tagged_logging.rb:69:in `block in tagged'activesupport (5.0.7.1) lib/active_support/tagged_logging.rb:26:in `tagged'activesupport (5.0.7.1) lib/active_support/tagged_logging.rb:69:in `tagged'railties (5.0.7.1) lib/rails/rack/logger.rb:24:in `call'newrelic_rpm (6.3.0.355) lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'sprockets-rails (3.2.1) lib/sprockets/rails/quiet_assets.rb:13:in `call'newrelic_rpm (6.3.0.355) lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'actionpack (5.0.7.1) lib/action_dispatch/middleware/request_id.rb:24:in `call'newrelic_rpm (6.3.0.355) lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'rack (2.0.6) lib/rack/method_override.rb:22:in `call'newrelic_rpm (6.3.0.355) lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'rack (2.0.6) lib/rack/runtime.rb:22:in `call'newrelic_rpm (6.3.0.355) lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'activesupport (5.0.7.1) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'newrelic_rpm (6.3.0.355) lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'actionpack (5.0.7.1) lib/action_dispatch/middleware/executor.rb:12:in `call'newrelic_rpm (6.3.0.355) lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'actionpack (5.0.7.1) lib/action_dispatch/middleware/static.rb:136:in `call'newrelic_rpm (6.3.0.355) lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'rack (2.0.6) lib/rack/sendfile.rb:111:in `call'newrelic_rpm (6.3.0.355) lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'railties (5.0.7.1) lib/rails/engine.rb:522:in `call'newrelic_rpm (6.3.0.355) lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'puma (3.12.0) lib/puma/configuration.rb:225:in `call'puma (3.12.0) lib/puma/server.rb:658:in `handle_request'puma (3.12.0) lib/puma/server.rb:472:in `process_client'puma (3.12.0) lib/puma/server.rb:332:in `block in run'puma (3.12.0) lib/puma/thread_pool.rb:133:in `block in spawn_thread'
更新:(更新 3)
如果我将控制器代码更改为:
- 上面的堆栈跟踪错误消失了
- 页面没有刷新 - 这很好
- 但仍然没有出现 Flash 消息
respond_to do |format|format.json { flash.now[:alert] = "报告生成已启动.您将在大约 4 - 5 分钟后收到一封电子邮件,其中包含下载报告的链接."}结尾
解决方案
您应该在表单中使用 remote: true
.
<%= form_tag generate_report_path(:header => true), remote: true do |f|% ><div class="container-fluid"><div style="padding-right:10px"><%= select_tag(:report_id, options_for_select([[选择报告类型", 0],[报告1",1],[报告2",2],["Report3", 3]]), id: "report_selection")%><%= hidden_field_tag :format, :pdf %><%= button_tag生成报告",类:'btn btn-sm btn-primary'%><%= label_tag do %><%= check_box_tag "format_reqd", "format_reqd", false %>检查数字格式<%结束%><%结束%>
<块引用>
Above remote: true
负责发送ajax请求到你的控制器方法.这是 form_for 和 remote 的完整指南请求.
在您的控制器方法中:
def generate_report... #无论你想在这里添加什么.render js: "alert('你的报告将在几分钟内通过电子邮件发送给你')"结尾
更新快讯:
您的控制器方法现在看起来像:
def generate_report... #无论你想在这里添加什么.response_to do |格式|format.js { flash[:notice] = "您的报告将在几分钟内通过电子邮件发送给您" }结尾结尾
渲染视图:
# app/views/users/generate_report.js.erb$("#flash").html('<%= j 渲染部分: "shared/notice_banner" %>');
<预><代码># app/views/layouts/application.html.erb<div id="闪存"><% if notice.present?%><%=渲染部分:shared/notice_banner"%><%结束%>
# app/views/shared/_notice_banner.html.erb<div data-alert class="alert-box"><%=通知%><a href="#" class="close">×</a>
这里是详细的答案.希望这会有所帮助.
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"; but the page should not be refreshed.
How can I achieve these two:
- Disable page refresh?
- Display a flash message?
Please help!
UPDATE:
- I added this to my controller (called
report_controller
):
def reportgen
respond_to do |format|
format.js { 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 these are the results for my 2 issues in my original question right now:
- Disable page refresh - this works perfectly fine now
- Display flash message - this does not work!
UPDATE:
I also see this error now (not sure what's causing this though):
ActionController::UnknownFormat (ReportController#reportgen is missing a template for this request format and variant.
request.formats: ["application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"]
request.variant: []):
actionpack (5.0.7.1) lib/action_controller/metal/implicit_render.rb:43:in `default_render'
actionpack (5.0.7.1) lib/action_controller/metal/basic_implicit_render.rb:4:in `block in send_action'
actionpack (5.0.7.1) lib/action_controller/metal/basic_implicit_render.rb:4:in `tap'
actionpack (5.0.7.1) lib/action_controller/metal/basic_implicit_render.rb:4:in `send_action'
actionpack (5.0.7.1) lib/abstract_controller/base.rb:188:in `process_action'
actionpack (5.0.7.1) lib/action_controller/metal/rendering.rb:30:in `process_action'
actionpack (5.0.7.1) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (5.0.7.1) lib/active_support/callbacks.rb:126:in `call'
activesupport (5.0.7.1) lib/active_support/callbacks.rb:506:in `block (2 levels) in compile'
activesupport (5.0.7.1) lib/active_support/callbacks.rb:455:in `call'
activesupport (5.0.7.1) lib/active_support/callbacks.rb:448:in `block (2 levels) in around'
activesupport (5.0.7.1) lib/active_support/callbacks.rb:286:in `block (2 levels) in halting'
audited (4.8.0) lib/audited/sweeper.rb:14:in `around'
activesupport (5.0.7.1) lib/active_support/callbacks.rb:405:in `public_send'
activesupport (5.0.7.1) lib/active_support/callbacks.rb:405:in `block in make_lambda'
activesupport (5.0.7.1) lib/active_support/callbacks.rb:285:in `block in halting'
activesupport (5.0.7.1) lib/active_support/callbacks.rb:447:in `block in around'
activesupport (5.0.7.1) lib/active_support/callbacks.rb:455:in `call'
activesupport (5.0.7.1) lib/active_support/callbacks.rb:448:in `block (2 levels) in around'
activesupport (5.0.7.1) lib/active_support/callbacks.rb:286:in `block (2 levels) in halting'
audited (4.8.0) lib/audited/sweeper.rb:14:in `around'
activesupport (5.0.7.1) lib/active_support/callbacks.rb:405:in `public_send'
activesupport (5.0.7.1) lib/active_support/callbacks.rb:405:in `block in make_lambda'
activesupport (5.0.7.1) lib/active_support/callbacks.rb:285:in `block in halting'
activesupport (5.0.7.1) lib/active_support/callbacks.rb:447:in `block in around'
activesupport (5.0.7.1) lib/active_support/callbacks.rb:455:in `call'
activesupport (5.0.7.1) lib/active_support/callbacks.rb:101:in `__run_callbacks__'
activesupport (5.0.7.1) lib/active_support/callbacks.rb:750:in `_run_process_action_callbacks'
activesupport (5.0.7.1) lib/active_support/callbacks.rb:90:in `run_callbacks'
actionpack (5.0.7.1) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (5.0.7.1) lib/action_controller/metal/rescue.rb:20:in `process_action'
actionpack (5.0.7.1) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
activesupport (5.0.7.1) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (5.0.7.1) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (5.0.7.1) lib/active_support/notifications.rb:164:in `instrument'
actionpack (5.0.7.1) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (5.0.7.1) lib/action_controller/metal/params_wrapper.rb:248:in `process_action'
activerecord (5.0.7.1) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (5.0.7.1) lib/abstract_controller/base.rb:126:in `process'
actionview (5.0.7.1) lib/action_view/rendering.rb:30:in `process'
actionpack (5.0.7.1) lib/action_controller/metal.rb:190:in `dispatch'
actionpack (5.0.7.1) lib/action_controller/metal.rb:262:in `dispatch'
actionpack (5.0.7.1) lib/action_dispatch/routing/route_set.rb:50:in `dispatch'
actionpack (5.0.7.1) lib/action_dispatch/routing/route_set.rb:32:in `serve'
actionpack (5.0.7.1) lib/action_dispatch/journey/router.rb:39:in `block in serve'
actionpack (5.0.7.1) lib/action_dispatch/journey/router.rb:26:in `each'
actionpack (5.0.7.1) lib/action_dispatch/journey/router.rb:26:in `serve'
actionpack (5.0.7.1) lib/action_dispatch/routing/route_set.rb:727:in `call'
newrelic_rpm (6.3.0.355) lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
omniauth (1.4.2) lib/omniauth/strategy.rb:186:in `call!'
omniauth (1.4.2) lib/omniauth/strategy.rb:164:in `call'
newrelic_rpm (6.3.0.355) lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
newrelic_rpm (6.3.0.355) lib/new_relic/rack/agent_hooks.rb:30:in `traced_call'
newrelic_rpm (6.3.0.355) lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
newrelic_rpm (6.3.0.355) lib/new_relic/rack/browser_monitoring.rb:32:in `traced_call'
newrelic_rpm (6.3.0.355) lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
rack (2.0.6) lib/rack/etag.rb:25:in `call'
newrelic_rpm (6.3.0.355) lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
rack (2.0.6) lib/rack/conditional_get.rb:38:in `call'
newrelic_rpm (6.3.0.355) lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
rack (2.0.6) lib/rack/head.rb:12:in `call'
newrelic_rpm (6.3.0.355) lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
rack (2.0.6) lib/rack/session/abstract/id.rb:232:in `context'
rack (2.0.6) lib/rack/session/abstract/id.rb:226:in `call'
newrelic_rpm (6.3.0.355) lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
actionpack (5.0.7.1) lib/action_dispatch/middleware/cookies.rb:613:in `call'
newrelic_rpm (6.3.0.355) lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
activerecord (5.0.7.1) lib/active_record/migration.rb:553:in `call'
newrelic_rpm (6.3.0.355) lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
actionpack (5.0.7.1) lib/action_dispatch/middleware/callbacks.rb:38:in `block in call'
activesupport (5.0.7.1) lib/active_support/callbacks.rb:97:in `__run_callbacks__'
activesupport (5.0.7.1) lib/active_support/callbacks.rb:750:in `_run_call_callbacks'
activesupport (5.0.7.1) lib/active_support/callbacks.rb:90:in `run_callbacks'
actionpack (5.0.7.1) lib/action_dispatch/middleware/callbacks.rb:36:in `call'
newrelic_rpm (6.3.0.355) lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
actionpack (5.0.7.1) lib/action_dispatch/middleware/executor.rb:12:in `call'
newrelic_rpm (6.3.0.355) lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
actionpack (5.0.7.1) lib/action_dispatch/middleware/remote_ip.rb:79:in `call'
newrelic_rpm (6.3.0.355) lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
actionpack (5.0.7.1) lib/action_dispatch/middleware/debug_exceptions.rb:49:in `call'
newrelic_rpm (6.3.0.355) lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
web-console (3.7.0) lib/web_console/middleware.rb:135:in `call_app'
web-console (3.7.0) lib/web_console/middleware.rb:22:in `block in call'
web-console (3.7.0) lib/web_console/middleware.rb:20:in `catch'
web-console (3.7.0) lib/web_console/middleware.rb:20:in `call'
newrelic_rpm (6.3.0.355) lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
actionpack (5.0.7.1) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
newrelic_rpm (6.3.0.355) lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
railties (5.0.7.1) lib/rails/rack/logger.rb:36:in `call_app'
railties (5.0.7.1) lib/rails/rack/logger.rb:24:in `block in call'
activesupport (5.0.7.1) lib/active_support/tagged_logging.rb:69:in `block in tagged'
activesupport (5.0.7.1) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (5.0.7.1) lib/active_support/tagged_logging.rb:69:in `tagged'
railties (5.0.7.1) lib/rails/rack/logger.rb:24:in `call'
newrelic_rpm (6.3.0.355) lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
sprockets-rails (3.2.1) lib/sprockets/rails/quiet_assets.rb:13:in `call'
newrelic_rpm (6.3.0.355) lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
actionpack (5.0.7.1) lib/action_dispatch/middleware/request_id.rb:24:in `call'
newrelic_rpm (6.3.0.355) lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
rack (2.0.6) lib/rack/method_override.rb:22:in `call'
newrelic_rpm (6.3.0.355) lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
rack (2.0.6) lib/rack/runtime.rb:22:in `call'
newrelic_rpm (6.3.0.355) lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
activesupport (5.0.7.1) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
newrelic_rpm (6.3.0.355) lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
actionpack (5.0.7.1) lib/action_dispatch/middleware/executor.rb:12:in `call'
newrelic_rpm (6.3.0.355) lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
actionpack (5.0.7.1) lib/action_dispatch/middleware/static.rb:136:in `call'
newrelic_rpm (6.3.0.355) lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
rack (2.0.6) lib/rack/sendfile.rb:111:in `call'
newrelic_rpm (6.3.0.355) lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
railties (5.0.7.1) lib/rails/engine.rb:522:in `call'
newrelic_rpm (6.3.0.355) lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
puma (3.12.0) lib/puma/configuration.rb:225:in `call'
puma (3.12.0) lib/puma/server.rb:658:in `handle_request'
puma (3.12.0) lib/puma/server.rb:472:in `process_client'
puma (3.12.0) lib/puma/server.rb:332:in `block in run'
puma (3.12.0) lib/puma/thread_pool.rb:133:in `block in spawn_thread'
UPDATE: (update 3)
If I change my controller code to this:
- the above stacktrace error goes away
- page refresh does not happen - this is good
- but the flash message still does not appear
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
解决方案
You should use remote: true
in your form.
<%= form_tag generate_report_path(:header => true), remote: 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 %>
<% end %>
Above remote: true
is responsible for sending ajax request to your
controller method. Here is a complete guide for form_for and remote
requests.
In your method of the controller:
def generate_report
... #Whatever you want to add here.
render js: "alert('Your report would be emailed to you in a few minutes')"
end
Update with the flash message:
Your controller method will now look like:
def generate_report
... #Whatever you want to add here.
respond_to do |format|
format.js { flash[:notice] = "Your report would be emailed to you in a few minutes" }
end
end
Rendered view:
# app/views/users/generate_report.js.erb
$("#flash").html('<%= j render partial: "shared/notice_banner" %>');
# app/views/layouts/application.html.erb
<div id="flash">
<% if notice.present? %>
<%= render partial: "shared/notice_banner" %>
<% end %>
</div>
# app/views/shared/_notice_banner.html.erb
<div data-alert class="alert-box">
<%= notice %>
<a href="#" class="close">×</a>
</div>
Here is the detailed answer present.
Hope this will help.
这篇关于在 Rails 中,是否可以在不调用 redirect_to 或 render 的情况下显示警报?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!