1
votes

I have a problem with flash[:notice] = "Message" in Ruby on Rails.

I am trying to create login fault error message. My login fault handling is:

flash[:notice] = "Invalid username/password combination."
redirect_to(:action => 'login')

For the reason I don't know, alert just doesn't show up. I have red tons of possible solutions, but all of them just doesn't work for me. I am using Safari / Google Chrome web browsers.

1
Where are you displaying flash messages in the view? What does your view look like?Chris Peters
No. In a controller. This alert is in a if statement and works if user is not authorised.Daumantas Versockas
I think @ChrisPeters was asking you to basically show us how you're displaying the alerts in your HTML. If he wasn't, than I am :)Sunil D.
what is your view? how you are rending flash message?Rubyrider
Sorry, guys, I don't understand you... I have red, that flash is some kind of error alert. So, thats what I am doing...Daumantas Versockas

1 Answers

2
votes

Your controller code looks fine. I suspect your problem is not that you are calling the flash method incorrectly in your controller, but rather that you did not write the code (correctly) to display the flash in your view.

flash simply holds a message for your views to display. You are setting the message correctly, but you may not be displaying it properly. I can't tell because you aren't posting your view code.

For example, if this action is the result of submitting a user/login.html.erb form, based on your controller code, you would want to have the following code inside that view file:

<% flash.each do |key, value| %>
  <div class="alert"><%= value %></div>
<% end %>

This code is a more basic version of what is described in this article.