Search code examples
ruby-on-railsruby-on-rails-3flashtwitter-bootstrap

Flash messages appearing twice in rails


My flash messages are appearing twice and my web research tells me this is due to render and redirect displaying the messages. I think I need to use flash.now[] or flash[] somewhere to sort this but I can't work out 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

Solution

  • You can do something like this in order to save some lines of code, and display the messages just 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 %>
    

    By using flash.discard, you show the flash message an avoid rendering twice