1

There's a few of these questions in SO but none address my specific issue so I'm posting it.

I'm trying to display flash notices for success and errors in my rails project but they aren't displaying and I don't know why.

class CheckinsController < ApplicationController
  def index
    @checkins = Checkin.all
    @checkin = Checkin.new

  end

  def create
      @checkin = Checkin.create(params[:checkin])
        if @checkin.errors.empty?
          render json: @checkin, status: 201,
          :notice => 'Thanks for posting your comments.'
        else
          flash[:notice] = "Please enter a name."
          redirect_to checkins_path
        end
  end
end

Note that yes, there's a discontinuity of :notice and flash[:notice] but it's the only way the controller doesn't break it.

My model looks like this and is doing what it should, that is, prevents a save if the name field in the form is vacant:

class Location < ActiveRecord::Base
   attr_accessible :name, :description

    has_many :checkins  

    validates :name, presence: true
end

And I have this in my application.html.erb file:

<div id="notice"><%= flash[:notice] %></div>

Technically this should work, it should display the notice when the error is found. Is there something I'm missing?

I've also tried using...

 <% flash.each do |name, msg| -%>
     <%= content_tag :div, msg, class: name %>
 <% end -%>

per RoR docs to no avail.

One thing to note is that it's also not rendering its CSS either. Seems related but I can't figure out how.

PanicBus
  • 566
  • 1
  • 7
  • 17

1 Answers1

1

This has been answered here: Rails Flash.now not working

Basically you need to call the method in a different manner in the view.

flash.now[:notice]

Would be the correct invocation.

Community
  • 1
  • 1
muttonlamb
  • 6,341
  • 3
  • 26
  • 35