Nick Res
Nick Res

Reputation: 2244

RoR's flash feature isn't working

In the signin form as users clicks the signin button to signin, the signin form calls the create action via the sessions resource.

Here is the action:

 def create
   user = User.find_by_email(params[:session][:email].downcase)
   if user && user.authenticate(params[:session][:password])
     #Sign the user in and redirect to user's main page
   else
    flash[:error] = "Invalid email/password combination"
    render 'new'
  end
 end      

Although the flash[:error] isn't working.

I tried placing a 'flash[:notice]' in the static pages controller to flash a notice whenever the home page is redirected to, although that doesn't work either.

My test suit gives me this error

Failure/Error: it { should have_selector('div.alert.alert-error', text: 'Invalid') }
   expected css "div.alert.alert-error" with text "Invalid" to return something

Is this a problem with my css?

Thanks for your help!

Upvotes: 0

Views: 155

Answers (2)

Chris Peters
Chris Peters

Reputation: 18090

Did you add code to display the flash in your layout or view template?

It looks like maybe you're using Bootstrap, so you could do something similar to this in your layout file (views/layouts/application.html.erb or other layout):

<% flash.each do |key, message| %>
  <div id="flash-<%= key %>" class="alert alert-<%= key %> fade in">
    <button class="close" data-dismiss="alert">&times;</button>
    <%= message %>
  </div>
<% end %>

Upvotes: 2

Svilen
Svilen

Reputation: 2648

You probably have to use flash.now[:error], since you're rendering the view aftewards.

flash.now[:error] = "Invalid email/password combination"

http://apidock.com/rails/ActionDispatch/Flash/FlashHash/now

Upvotes: 2

Related Questions