0

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:

  1. Disable page refresh?
  2. Display a flash message?

Please help!

UPDATE:

  1. 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
  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 these are the results for my 2 issues in my original question right now:

  1. Disable page refresh - this works perfectly fine now
  2. 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
m.beginner
  • 389
  • 2
  • 18

1 Answers1

2

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

Here is the detailed answer present. Hope this will help.

NN796
  • 1,247
  • 11
  • 31
  • @m.beginner if the answer helps you out please accept this answer. Thank you – NN796 Jun 24 '19 at 12:47
  • `render js: "alert('Your report would be emailed to you in a few minutes')"` This generates a popup. Is there a way to generate a flash message without a popup? – m.beginner Jun 24 '19 at 13:14
  • Thanks for the reply. I already have this in my `application.html.erb`: `<% flash.each do |key, value| %> <% if key.to_s == "alert" %>
    <%= value %>
    <% else %>
    <%= value %>
    <% end %> <% end %>` With the above code already present, do I still need `_notice_banner.html.erb`?
    – m.beginner Jun 24 '19 at 14:02
  • @m.beginner no you don't need `_notice_banner.html.erb` anymore. – NN796 Jun 24 '19 at 14:06
  • So I believe I don't need the change you pointed out in `application.html.erb` either? If the above statement is correct, then should this line be changed to render `application.html.erb` instead of `shared/notice_banner`?: `$("#flash").html('<%= j render partial: "shared/notice_banner" %>');` If yes, how do I change the above line? – m.beginner Jun 24 '19 at 14:12
  • @m.beginner please ignore my last comment. You need to update the `application.html.erb` same as I have added above in my answer and update your `generate_report.js.erb` in the same manner. Shortly, if you move your flash message rendering code in a partial then it will help you render that partial again from anywhere i.e. the reuseability will be achived. Hope this will help you out. – NN796 Jun 24 '19 at 14:28
  • @m.beginner does this all helped you out? – NN796 Jun 25 '19 at 05:19
  • I tried all the changes you mentioned. But unfortunately the flash message does not appear at all (but page refresh is disabled which is a good thing) – m.beginner Jun 25 '19 at 06:57
  • @m.beginner can you update your question with the issue of the flash message not rendering. – NN796 Jun 25 '19 at 06:59
  • @m.beginner please add your code with a new heading of "UPDATE" in your question. Also, 1. Is your controller method rendered a flash message? 2. Do you have `generate_report.js.erb` updated similar to my answer? 3. Does your `application.html.erb` is updated the same as in my answer? 4. Have you added `_notice_banner.html.erb`? 5. Is your `_notice_banner.html.erb` rendered in `application.html.erb` with the correct path? 6. Can you debug your front end view by adding `<%= debugger %>` or `<%= binding.pry %>` and check is the flash object has correct message in it? – NN796 Jun 25 '19 at 07:16
  • I've added `UPDATE` section with all my relevant code snippets as you suggested – m.beginner Jun 25 '19 at 07:30
  • @m.beginner can you add a `<%= debugger %>` or `<%= binding.pry %>` in your `application.html.erb` before `<% if notice.present? %>` line. Also, when you submit the form then the execution should be stopped at debugger at this mentioned place. If debugger stopped please check what is present in the alert object? Is the error is present in the alert object? You need to debug this why it is not showing flash alerts. You are very close to the solution however there must be a minor thing which you are missing here. – NN796 Jun 25 '19 at 09:40
  • I just added did this in `application.html.erb` - `<%if alert.present?%><%puts "alert present"%>...<%else%><%puts "alert NOT present"%><%end%>`. I guess this should do for a debug? and interestingly, I could not find either of the messages (neither the one inside if nor the one inside else) in the rails console. What would this mean? – m.beginner Jun 25 '19 at 10:07
  • @m.beginner This means that your flash message is not set from the controller. Can you please use `flash[:alert]` instead `flash.now[:alert]` in controller method. Source: https://stackoverflow.com/questions/7133392/rails-flash-now-not-working https://stackoverflow.com/questions/32690339/flashalert-not-working-but-flashnotice-displays-the-message-on-redirect – NN796 Jun 25 '19 at 10:19
  • I tried `flash[:alert]` instead of `flash.now[:alert]` - but I still have the same issue. Neither if nor else seems to be executed. Also, I just noticed I am getting an error stacktrace. I have posted the stacktrace as an UPDATE to my original question. I am getting this error for both `flash.now[:alert]` and `flash[:alert]` – m.beginner Jun 25 '19 at 11:56
  • The first line of the error suggests it is definitely coming from the controller method where I am setting the flash message - `ActionController::UnknownFormat (ReportController#reportgen is missing a template for this request format and variant. ` – m.beginner Jun 25 '19 at 12:10
  • @m.beginner here are some details https://stackoverflow.com/questions/22943892/actioncontrollerunknownformat Please check your server logs right after clicking the `generate report` button on the browser. Please check in the server logs if it is mentioned `as json` or `as js`? If it is `as json` then you need to add `format.json` at your backend. – NN796 Jun 25 '19 at 13:00
  • I tried `format.json` instead of `format.js` - I have posted the code for this and the results in `UPDATE: (update 3)` in my original question above. – m.beginner Jun 25 '19 at 13:10
  • @m.beginner is the error gone from server logs? And can you try `flash[:notice]` instead of `flash.now[:alert]` – NN796 Jun 25 '19 at 13:12
  • @m.beginner if this is still unresolved then please add a separate question on StackOverflow with new updated details. – NN796 Jun 25 '19 at 13:34
  • This is still unresolved (even after trying `flash[:alert]` insetad of `flash.now[:alert]`. I have opened a new question with the latest update here https://stackoverflow.com/questions/56756173/how-to-display-flash-message-without-redirection-in-ruby-on-rails as you suggested – m.beginner Jun 25 '19 at 14:20
  • I have edited the question in the new thread to focus only on the flash message issue (and NOT the page refresh issue). Pls help! – m.beginner Jun 26 '19 at 09:20