0

this code generated by scaffold ends up printing the same message twice and i can't get it to stop.

  def update
    respond_to do |format|
      if @indi.update(indi_params)
        puts("message about to be shown to us by the system")
        format.html { redirect_to @indi, notice: 'Indi was successfully updated.' }
        format.json { render :show, status: :ok, location: @indi }
      else
        format.html { render :edit }
        format.json { render json: @indi.errors, status: :unprocessable_entity }
      end
    end
  end

it prints the success message twice in the web page.

kiddorails
  • 12,961
  • 2
  • 32
  • 41
  • in your webpage view are you rendering the notice twice – kiddorails Jun 21 '18 at 18:58
  • not on purpose. here is some console output from the server. message about to be shown to us by the system Redirected to http://localhost:3000/indis/48 Completed 302 Found in 5ms (ActiveRecord: 0.0ms) Started GET "/indis/48" ... Processing by IndisController#show as HTML Parameters: {"id"=>"48"} User Load (0.0ms) SELECT "users".* FROM Indi Load (0.0ms) SELECT "indis".* FROM "indis" WHERE "indis"."id" = ? LIMIT ? [["id", 48]... Rendering indis/show.html.erb w/in layouts/application Rendered indis/show.html.erb w/iin layouts/application – user1747236 Jun 21 '18 at 19:04
  • [this code found in application.html.erb <% if notice %> <%= notice %> <% end %> <% if alert %>

    <%= alert %>

    <% end %> <%= yield %>
    – user1747236 Jun 21 '18 at 19:05
  • top of view has

    <%= notice %>

    – user1747236 Jun 21 '18 at 19:07
  • so you are doing it twice, right. I see it in alert-success as well as _top of view_. isn't that the issue for double rendering? – kiddorails Jun 21 '18 at 19:07
  • i do see code about notices in application.html.erb as well as my view show.html.erb . maybe relevant? – user1747236 Jun 21 '18 at 19:07
  • i think you are on to something – user1747236 Jun 21 '18 at 19:08
  • yes, it is. Do it at one place. Do it in application.html.erb alone since showing notice is general thing, to be shared across views. – kiddorails Jun 21 '18 at 19:08
  • thank you so much that worked! (new to Rails) – user1747236 Jun 21 '18 at 19:19
  • sure. glad to help – kiddorails Jun 21 '18 at 19:20

1 Answers1

1

To close the question:

Problem was in double rendering in the layout as well as view. He had:

<body>
  <% if notice %>
    <p class="alert alert-success"><%= notice %></p>
  <% end %>
  <% if alert %>
    <p class="alert alert-danger"><%= alert %></p>
  <% end %> 
  <%= yield %> 
</body>

and in top of show.html.erb:

<p id="notice"><%= notice %></p>

This resulted in double display of notice. Fix was to simply remove from show.html.erb and to have that in layout. Layout enables such code to be re-used

kiddorails
  • 12,961
  • 2
  • 32
  • 41