I have created a simple application with Foundation 5 and Devise support. I noticed that messages for Sign In and Sign Up failures are displayed differently. Foundation 5 has generated the following partial for displaying flash messages:
<% flash.each do |name, msg| %>
<% if msg.is_a?(String) %>
<div data-alert class="alert-box round <%= name == :notice ? "success" : "alert" %>">
<%= content_tag :div, msg %>
<a href="#" class="close">×</a>
</div>
<% end %>
<% end %>
I have Googled and read many posts but none of them answer my questions.
I have the following questions:
1) As far as I understand Sign In failure messages are automatically put in the flash hash while Sign Up failure messages are not. Am I right?
2) If so, what is rationale behind it? I mean, why are Sign In failure messages are automatically added to the flash but Sign Up and Send Me Reset Password are not?
3) I want to put all generated Devise messages into the flash so that the partial will display it. So far, the only way I have come up with is to override devise_error_messages!
,scan the resource.errors.full_messages
and put them into flash. Does anyone know a better method to do it?
This is how I overrode devise_error_messages!
:
module DeviseHelper
def devise_error_messages!
return "" if resource.errors.empty?
flash.now[:error] = resource.errors.full_messages.first
return ""
end
end
1) As far as I understand Sign In failure messages are automatically put in the flash hash while Sign Up failure messages are not. Am I right?
Indeed.
2) If so, what is rationale behind it? I mean, why are Sign In failure messages are automatically added to the flash but Sign Up and Send Me Reset Password are not?
When creating/updating the User model (or the model used for Devise), the error are going to be showed like form errors. Login/Logout doesn't modify the model, so a flash message is shown instead.
Also, (particularly for Sign Up
) showing a big unformatted paragraph with several error messages at the top (where usually the flash messages lie) doesn't seem like a good idea, compared to having a special <div>
just on top of the form having the attributes and errors showed in a pretty format.
3) I want to put all generated Devise messages into the flash so that the partial will display it. So far, the only way I have come up with is to override devise_error_messages! ,scan the resource.errors.full_messages and put them into flash. Does anyone know a better method to do it?
If you still want to use a flash message to show the error messages, the code you're showing only gives you the first error message, not all.
Try something like this:
flash.now[:error] = resource.errors.full_messages.join(" ")
Or even better, since devise_error_messages!
is called from the view for the form:
flash[:error] = resource.errors.full_messages.join(" ")
on the Controller.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.