Flash messages appearing twice on rails

My flash messages appear twice, and my web research tells me that this is related to displaying and redirecting messages. I think I need to use flash.now [] or flash [] somewhere to sort this out, but I can't decide where it needs to go

guidelines_controller.rb

def update
  @guideline = Guideline.find(params[:id])

  respond_to do |format|
    if @guideline.update_attributes(params[:guideline])
      @guideline.update_attribute(:updated_by, current_user.id)
      format.html { redirect_to @guideline, notice: 'Guideline was successfully updated.' }
      format.json { head :no_content }
    else
      format.html { render action: "show" }
      format.json { render json: @guideline.errors, status: :unprocessable_entity }
    end
  end
end

Layouts / application.html.erb

<div class="container">

    <% flash.each do |type, message| %>

        <div class="alert <%= flash_class type %>">
            <button class="close" data-dismiss="alert">x</button>
            <%= message %>
        </div>
    <% end %>
</div>

application_helper.rb

def flash_class(type)
  case type
  when :alert
    "alert-error"
  when :notice
    "alert-success"
  else
    ""
  end
end

guideline_controller.rb

def show
    @guideline = Guideline.find(params[:id])
    if @guideline.updated_by
     @updated = User.find(@guideline.updated_by).profile_name
   end

      if User.find(@guideline.user_id)
     @created = User.find(@guideline.user_id).profile_name
      end

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @guideline }

    end
  end
+5
source share
1 answer

You can do something like this to save some lines of code and display messages only once:

<%- if flash.any? %>
  <%- flash.keys.each do |flash_key| %>
    <%- next if flash_key.to_s == 'timedout' %>
    <div class="alert-message <%= flash_key %>">
      <a class="close" data-dismiss="alert" href="#"> x</a>
      <%= flash.discard(flash_key) %>
    </div>
  <%- end %>
<%- end %>

Using flash.discard you show flash message, avoid rendering twice

+1
source

All Articles