The "Rails Way" to Handle field_error_proc and Field with Error

Hey everyone!

I was recently playing around with rendering inline errors using the normal Ruby on Rails view stack, when I came across this blog post. There’s certainly some interesting stuff in there, but one of the things that stuck out to me particularly is how the field_error_proc attached to the base ActionView class can be modified to render inline errors by default.

I did some looking around to try and learn more about how this proc is inserted into forms built with the Rails form helpers, and I struggled to find information on the topic. I couldn’t find anything exposed on the API docs, and the only thing that I found in Rails Guides is this section on changing the proc using the Rails’ configuration features. A particularly frustrating part of this is not knowing how the Rails form builder processes this proc, and what methods the default arguments for this proc respond to.

I was wondering if anyone could provide some historical context as to why this feature exists as part of the form builder, and if there’s a prescribed way to interact with it. Given current trends in web design, it seems like this could be a really useful feature, but it seems to be something that’s difficult to find information on, and that many people don’t take advantage of.

I hope that this topic can spark some conversation around the feature. I’d love to hear all of your experiences or insights into this topic.

I don’t have much to offer as a historical context, but I’ve recently worked on form building and am looking into preparing a PR for

It could be helpful If you have an input of what the implementation in this PR should handle.

1 Like

Hey! Thanks for taking the time to share your thoughts on the subject.

I think rolling something like this into the form builder API could work really well! I think this could also help cover the gap in documentation for the feature.

I’d love to offer some commentary or assistance with the PR, but unfortunately I’m a pretty amateur developer. If you have some kind of direction or architecture suggestions, I think I could be able to help from there. I just don’t want to offer uneducated advice.

New doc were added, though still a bit lacking.

html_tag is just the string representing the inner tag, but instance isn’t very clear. I logged what it is, and think they are all subclasses of ActionView::Helpers::Tags::Base representing the one kind of tag. This isn’t very well documented either, but the object being the activemodel itself means we can access errors information.

#<ActionView::Helpers::Tags::Label:0x000000012f11d318 
@content=nil, 
@method_name="name", 
@object_name="project", 
@template_object=#<ActionView::Base:0x000000003017e0>, 
@object=#<Project id:20 user2group3/f1>>, 
@skip_default_ids=false, 
@allow_method_names_outside_object=false, 
@options={:class=>"label-bold", :for=>"project_name_edit"}, 
@generate_indexed_names=false, 
@auto_index=nil, 
@sanitized_method_name="name", 
@tag_builder=#<ActionView::Helpers::TagHelper::TagBuilder:0x000000012f11cd00>
>