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 %>
Related
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
My devise.en.yml has the sign_out message
sessions:
signed_in: "Signed in successfully."
signed_out: "Signed out successfully."
and I have a _messages.html.erb to check flash messages
<% flash.each do |name, msg| %>
<% if msg.is_a?(String) %>
<%= content_tag :div, msg, :class => "flash_#{name}" %>
<% end %>
<% end %>
and this is my signout button
<%= link_to 'Sign out', destroy_user_session_path, :method=>'delete' %>
All my other messages work correctly like signup, signin, etc, etc. The only one that doesnt work is my signout.
Why, and how can I fix it?
Ok, so for the people that was wondering what was wrong, my routing was messed up. after I sign out, the client was trying to access something it shouldn't therefore redirecting to another page while removing the "signed out successfully".
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.
In devise.en.yml,i kept signed_out message empty as
sessions:
signed_out: ""
i am having a condition where if failed attempt > 3 then we have to make user signed out.
For this i wrote in the controller as:
if params[:failed_attempt].to_i > 3
current_user.update_attribute(:status, false)
redirect_to destroy_user_session_path(#user), :notice =>
"locked"
end
I am able to update attribute and able to logout but couldnot able to display notice or flash message. Please try to help me out.
I am getting all the messages except this. I also tried :
redirect_to destroy_user_session_path(#user)
flash[:notice] = "locked"
But no use.
Most likely you are setting the flash variable but not actually rendering out the content... try this:
In your application.html.erb before the <%= yield %> paste in code like this:
<% flash.each do |name, msg| %>
<div data-alert id="flashes" class="alert-box <%= name %>">
<%= content_tag :div, msg, id: "flash_#{name}" %>
×
</div>
<% end %>
You can style it afterwards whatever way you want
This done the trick. I just changed my signout as:
sign_out(current_user), :notice =>"locked"
instead of
redirect_to destroy_user_session_path(#user), :notice => "locked"
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>