Hallo,
Could anyone share some advice on the topic subject?
I wonder what is the best way to display validation messages next to input they belong to.
So far I created a helper, which uses exposed array with those messages and checks if there are any messages to be displayed for this input. If there are, it generates some HTML with those messages and injects it in the place in template where it was called.
Template:
form_for :user, routes.login_path do text_field :login p display_errors :login text_field :password p display_errors :password submit 'Login' end
Helper:
module Panel module Helpers module FormError def display_errors(field) return nil if result.messages[field].nil? html.div do ul do result.messages[field].each do |m| li m.to_s end end end end end end end
Is this the proper way? Why do I need to prepend this helper with p to render the content of html returned by helper? Is it because it’s inside a form_for block? How do you deal with this?