I have two forms in one view updating the User Profile. One Form is to Update my name etc. and one is to change my password.
Profile Form:
- form_for @user, :url => account_path do |f|
= f.error_messages
...form fields here...
Password Form:
- form_for @user, :url => acc开发者_如何学Pythonount_path do |pf|
= pf.error_messages
...password fields here...
As you can see they both point to the same update action in the users controller. If I type in a invalid password both error_messages
show me the same error message.
How can I output separate error messages per form?
You need to use error_message_on
to output the error message for one specific attribute. Keep in mind, that its output is not very meaningful by itself, as it lacks the attribute name and the header message that error_messages
/error_messages_for
include by default. You can use :prepend_text and :append_text to customize the messages (look at the API docs for more info).
Note that for the password part, all you have to do is call error_message_on :password
, but for the rest of the form fields, you have to call error_message_on
once for every attribute, apart from :password. You could write your own helper to avoid that.
精彩评论