在Rails中,是否可以显示警报而无需调用redirect_to或render?

时间:2019-06-24 12:03:39

标签: ruby-on-rails

我有一个表格:

<%= 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按钮时,我想向用户显示警报:“您的报告将在几分钟内通过电子邮件发送给您”;但是页面不应该刷新。

如何实现这两个目标:

  1. 禁用页面刷新吗?
  2. 显示即时消息吗?

请帮助!

更新:

  1. 我将此添加到了我的控制器(称为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
  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. 这是我的代码,用于在表单中添加按钮和复选框:
                  <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>

使用上面的代码,这些是我当前第2个问题的结果:

  1. 禁用页面刷新-现在可以正常使用
  2. 显示即时消息-这不起作用!

更新:

我现在也看到此错误(虽然不确定是什么原因造成的):

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'

更新:(更新3)

如果我将控制器代码更改为此:

  • 上述堆栈跟踪错误消失
  • 页面刷新不会发生-这很好
  • 但Flash消息仍然没有出现
            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

1 个答案:

答案 0 :(得分:2)

您应在表单中使用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(
      [["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  %>
  

remote: true以上负责将ajax请求发送到您的   控制器方法。这是form_for和remote的完整guide   请求。

在您的控制器方法中:

def generate_report
  ... #Whatever you want to add here.
  render js: "alert('Your report would be emailed to you in a few minutes')"
end

使用即时消息更新

您的控制器方法现在将如下所示:

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

渲染视图:

# 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">&times;</a>
</div>

Here是当前的详细答案。 希望这会有所帮助。