Remove flash message on refresh of page - ruby-on-rails

I am using devise for log in and i have overwrite my devise session controller and showing flash messages if user enter wrong information using following code to display flash message.
<% flash.each do |name, msg| %>
<%= content_tag :section, msg, :id => "flash_#{name}", :class => " flash" %>
<% end %>
And it is working fine and showing "Invalid email or password." message if user enter wrong email and password .
Now it is still showing the same flash message if i refresh the page.
But i want the flash message to be remove if page gets refresh .please help me on same.

You can use flash.discard after it getting displayed. Possibly like:
<% flash.each do |name, msg| %>
<%= content_tag :section, msg, :id => "flash_#{name}", :class => " flash" %>
<% flash.discard(name) %>
<% end %>
I haven't tested this, so try if it works.

Related

Rails+Devise: No error message is shown

I'm making rails + Devise + Bootstrap app.
Basically it seems like working well, but when it gets wrong password
, no error message is shown like this.
I can't figure out why it happened and how to fix it.
Could you give me any advise for that? Thanks in advance!
Add these lines above the yield block in your application layout as suggested in one comment.
# In application.html.erb
<% flash.each do |name, msg| %>
# New code (allow for flash elements to be arrays)
<% if msg.class == Array %>
<% msg.each do |message| %>
<%= content_tag :div, message, :id => "flash_#{name}" %>
<% end %>
<% else %>
# old code
<%= content_tag :div, msg, :id => "flash_#{name}" %>
<% end %> #don't forget the extra end
<% end %>
Just add this code in the controller action, where you want to show the error messages,
flash[:notice] = flash[:notice].to_a.concat resource.errors.full_messages
This link gives you many answers for this

Flash message don't show up when I do return redirect_to

I've a weird problem(I'm still learning rails) with rails and flash messages.
I have a link_to with method post in my rails view:
<%= link_to "<i class='fa fa-times'></i>".html_safe, email_post_path(#post.id), method: 'post' %>
and in my controller I do this:
def email_post
# do nothing
return redirect_to posts_path, alert: 'test flash'
end
The problem
is that works fine(so it redirect me correctly), but don't show the flash message(and the problem is not about the showing flash message, but that the flash[:alert] doesn't exists)
This is the way I show the flash messages:
<%= binding.pry %> #used for see if the flash is empty or not - and in this case is it!
<% flash.each do |type, message| %>
<div class="flash-<%= type %>">
<%= message %>
</div>
<% end %>
Why don't show the flash message in this case ?
Ok the problem seems to be
error: 'lol'
With:
alert: 'lol'
Works fine...why ?

Detect incorrect user login inside view template

I use devise with Rails 4. In my sessions/new.html.erb how do I know that there was an incorrect user login attempt? All I want is to display an error message when there was an incorrect login attempt. For design reasons I can't use flash messages.
Update:
After doing some research I found that I can get error messages like this:
<%- flash.each do |name, msg| -%>
<%= content_tag :div, msg, :id => "flash_#{name}" if msg.is_a?(String) %>
<%- end -%>
But this is a poor design if I rely on contents of flash message to figure out whether login was successful. I also tried resource.errors but it's empty. This should be such a basic feature, I don't understand why it takes me so much time to figure it out.
this should work to check if you have errors under the view:
<% if resource.errors.present? %>
<%= devise_error_messages! %>
<% end %>
cheers.

add id or class to flash message

Is there a way to add a class or an id to a specific flash message? I need to have some messages fade away and some persist. I would like to do this based on a html class.
Thanks for any help
There is nothing magical about what can put in the flash. It's just a hash that is stuffed in the session and cleared out for you.
You could (for example) do
flash[:notice] = {:class => :urgent, :body => 'hello'}
And then in your layout
- if flash[:notice]
%div{:class => flash[:notice][:class]}
= flash[:notice][:body]
But there's definitely more than one way to do this - you could just use one class when it's flash[:notice], another for flash[:error] etc
Flash messages are simply stored in a hash, flash. In your view, you might iterate over all of your flash messages like this:
<% flash.each do |key, msg| %>
<%= content_tag :div, msg, :id => key %>
<% end %>
You could always check for a specific message and append a specific class. Maybe something like:
<% flash.each do |key, msg| %>
<% if msg.include? 'fatal' %>
<%= content_tag :div, msg, :id => key, :class => 'fatal' %>
<% end %>
<% end %>
Here's a railscast with more information on flash messages.
http://railscasts.com/episodes/18-looping-through-flash
Add a class:
<%= content_tag(:p, notice, class: 'notice') if notice %>
<%= content_tag(:p, alert, class: 'alert' ) if alert %>
Add an id:
<%= content_tag(:p, notice, id: 'notice') if notice %>
<%= content_tag(:p, alert, id: 'alert' ) if alert %>
It renders (example):
<p class="alert">Invalid email or password.</p>
<p id="alert">Invalid email or password.</p>

Rails Devise Flash[:notice] not showing up

A am new to devise, and just set it up with default functionality.
I cannot get the flash[:notice] messages to show up.
I put this in my application.html.erb file
<% flash.each do |name, msg| %>
<%= content_tag :div, msg, :id => "flash_#{name}" %>
<% end %>
I can get the error messages like "already logged in" but I cannot get the regular notice messages like "Signed in successfully."
Can anyone point me in the direction of why this is not working.
I just want to use the defaults I dont care about having custom notices.
Try the change below.
<%= content_tag :div, msg, :id => name %>

Resources