简体   繁体   中英

Handling Error Messages in Rails

Right now I'm using the following to output error messages in my Rails 3 app. This is located in the layout view under the header:

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

This works ok for most cases, but when I have a signin for, and the user enters invalid credentials, showing an error message in the header looks bad. It's to far from the form. And most sites show the error message right above the form?

How can I do that? Do you really have to specify this error block in all your views? Given how amazing rails is, I'm guessing there's some trick to make this work?

Thanks

If you're using form_for and passing it an instance of whatever object your form represents, it will automatically put the errors inline with the fields. You could even take that a step further and use the gem Formtastic . It takes care of building semantic HTML.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM