Rails: Modify Validation error reporting?
The standard Rails validation wraps my error-filled fields in a div
of class fieldWithErrors
.
How can I change it so that the erroneous element's parent gets assigned that class instead of having a new div
created on the page?
EDIT:
Per this website, this wraps errors in span
s instead of div
s, which helps my formatting a little, but I'd really love to just stick the error class in the parent element...
app/config/environment.rb
(in the initializer block):
config.action_view.field_error_proc = Proc.new { |html_tag, instance| %(<span class="fieldWithErrors">#{html_tag}</span>) }
The Hpricot method listed on that site loo开发者_Go百科ks like what I want, but I don't know where I should be putting it. I also keep getting uninitialized constant ActionView
errors too. Can someone help?
You can always just set the divs with the class fieldWithErrors to display inline. You can use CSS instead of trying to ovverride the validation's wrapping method.
Awesome!
I figured out how to get this post working (I know, I'm such a newbie).
Create a new file in app/config/initializers
; I called mine error_formatting.rb
.
Paste the optimized method from the website listed above in there:
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
...
end
Save. Throw in some hpricot with a pinch of config.gem 'hpricot'
in the app/config/environment.rb
intializer block, then rake gems:install
, restart the app, and now my app is working exactly the way I want it too, like a cute little kitten.
精彩评论