Fixing devise alert with a bootstrap alert - ruby-on-rails

Building it with rubies on Rails
When devise attempts to log in and it fails
it says alert type as (alert)
Am trying to change it to show Danger
GitHub commit changes
I apologize in advance only been doing this for Less 3 to 4 months just looking for some help
My attempt to use a helper class to try to fix the situation the first time
<% flash.each do |type, msg| %>
<% if type === 'alert' %>
<% type = 'danger' %>
<% end %>
<%= content_tag :div, msg, class: "alert alert-#{type}", role:"alert" %>
<% end %>
</div>

If I didn't get you wrong, you want to show a Devise alert styled as a Bootstrap alert. Try adding this piece of code to your main layout (it's usually at "app/views/layouts/application.html.erb")
<% if alert != nil%>
<p class="alert alert-danger"><%= alert %></p>
<%end%>

Related

How to add bootstrap flash messages to entire app via application.html.erb?

I've generated some resources via scaffold, and see <p id="notice"><%= notice %></p> at the top of index and show views.
I have searched for all occurrences of this across the app and deleted them (so there are no flash messages).
Then, I added this to application.html.erb
<% if notice %>
<p class="alert alert-success"><%= notice %></p>
<% end %>
<% if alert %>
<p class="alert alert-danger"><%= alert %></p>
<% end %>
Will this effectively generate all possible flash messages?
The reason I ask is because I believe bootstrap has >2 types of messages (e.g. "success", "danger", "info", "primary", "secondary", "warning", "light", "dark"), yet the above code seems to only cater to 2.
For reference, the added code comes from here
Devise just uses notice and alert. If you want more, just add it as described here. If you add warning, like in the docs, you can add:
<% if warning %>
<p class="alert alert-warning"><%= warning %></p>
<% end %>
and so on.

Strange output from rails each do

Rails each do method is acting strangely and I do not know why.
controller
def index
#fabric_guides = FabricGuide.with_attached_image.all.order(:name)
end
index.html.erb
<div class="guide-items">
<%= #fabric_guides.each do |fabric| %>
<div class="guide-container">
<%= link_to fabric_guide_path(slug: fabric.slug) do %>
<%= image_tag fabric.image if fabric.image.attached? %>
<% end %>
<div class="guide-info">
<p class="g-name">
<%= link_to fabric.name,
fabric_guide_path(slug: fabric.slug) %>
</p>
</div>
</div>
<% end %>
</div>
I have two FabricGuide records so I expect two "guide-container" but I get three. Or more precisely I get two guide containers and a third block of text containing all the content from the last FabricGuide record.
I have almost an identical setup for articles and have never encountered this problem. I'd happily share more information if needed. Thank you!
Please remove = equal sign from your each loop of view code
like below :-
<% #fabric_guides.each do |fabric| %>
...
...
<% end %>
you have used this <%= #fabric_guides.each do |fabric| %> in your view that's why it shows all record in DOM.
The expression for erb tags is <% %>
now if we want to print that tag too then we apply <%= %>

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

How do I get my Flash Message to show using bootstrap?

I have already imported bootstrap and I'm using the 'bootstrap-sass' gem and devise gem. The problem is My rails app shows the the right message like if am signed in and i try to sign up it shows the message 'You are already signed in.' but without the color around the message like yellow or red or any color.
<div class="container">
<% flash.each do |name, msg| %>
<%= content_tag(:div, msg, class: "alert alert-#{name}") %>
<% end %>
</div>
When I inspect the element in chrome after it rendered I get 'class="alert alert-alert"'
In my code above the "alert alert-#{name}" the name is the key which should change to info, success, or warning or danger. but it keeps changing to alert which bootstrap doesn't have. How can i fix this?
You can fix this by setting your flash message in your controller as flash[:success], flash[:info], flash[:warning] or flash[:danger], as opposed to flash[:alert].
For Devise, you will need to copy the Devise controllers into your Rails application to override these values. Alternatively, you can display the Bootstrap alerts manually if flash[:notice] or flash[:alert] are set:
<% if flash[:notice] %>
<div class="alert alert-info">
<%= flash[:notice] %>
</div>
<% end %>
<% if flash[:alert] %>
<div class="alert alert-danger">
<%= flash[:alert] %>
</div>
<% end %>

DEVISE - successful message when ask for a new password

I'm using the Devise gem and I just would like to show a successful message when someone ask for a new password (if forgotten). Currently, when submitted, the button redirects to sign_in without any message.
Thank you
Rather than using the flash and trying to work out how Devise does it's thing (not for the faint-hearted) by extending its controllers, how about checking the referrer, and displaying a message in the view if it matches your 'remind me of my password' path?
In the view:
<% if request.env['HTTP_REFERER'] == "/give/me/a/new/password" %>
<h2>Your password stuff is all good now.</h2>
<% end %>
flash[:success] = "Something Something"
Devise rolls out it's own alert messages (and they can be edited in the config/locales/devise.en.yml and written for other languages), you just need to catch them.
One way is to add a layout/_messages partial:
<% flash.each do |name, msg| %>
<% if msg.is_a?(String) %>
<div class="alert alert-<%= name %>">
<a class="close" data-dismiss="alert">×</a>
<%= content_tag :div, msg, :id => "flash_#{name}" %>
</div>
<% end %>
<% end %>
and render it in application.html.erb:
<%= render 'layouts/messages' %>
This has the benefit of catching all (devise and other) messages and playing nicely with bootstrap alert classes (if you use were using bootstrap).
.
Or if you are using slim:
- flash.each do |name, msg|
- if msg.is_a?(String)
div class="alert alert-#{name}"
a class="close" data-dismiss="alert"
| ×
= content_tag :div, msg, :id => "flash_#{name}"
= render 'layouts/messages'
.
Devise uses the rails standard :notice rather than :success but a you can add the success (green) styling to your css/scss (e.g. to bootstrap_and_overriders.css.scss):
.alert-alert {
#extend .alert-error
}
.alert-notice {
#extend .alert-success
}

Resources