开发者

Customising error handling with rails3

开发者 https://www.devze.com 2023-04-13 05:29 出处:网络
I want to take control of the way rails handles the display of errors, I want to keep using the FormBuilder and I don\'t want to use Formtastic (Love formtastic but like all things helpful it\'s great

I want to take control of the way rails handles the display of errors, I want to keep using the FormBuilder and I don't want to use Formtastic (Love formtastic but like all things helpful it's great until you want to go off piste - then you have to jump through spiky burning hoops with people firing guns at you to find out how to do it...). And I don't want to use the dynamic_form plugin to get access to the deprecated method 'error_messages_for' as this will cause me problems later on, despite being a quick fix for what I want to do.

I have read, several times, that Rails3 has changed the way it handles errors to allow one to easily customise it's behaviour but that's always as far as the documentation ever seems to get, neglecting the most important part of how this is done.

Does anyone know where I can find documentation that discusses how I can customise/control the display of errors... The lack of success using Google suggests to me that I have missed something embarrassingly obvious.

I realise that I have not said exactly what I want to do with the errors as I am more interested in learning about how to customise them rather than just a spinet of code to 开发者_JAVA技巧do what I want.


Ok - it is actually quite trivial once I actually read the documentation...

At the very least this works, for each field you can look at the errors, so with the example of the form builder something like this for each field (done in a helper some place for DRYness)

f.object.errors.each do |key, value|
    output << %(#{key.to_s.humanize} #{value.to_s}<br/>)              
  end
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号