Reputation: 798
I have a Rails Application which includes a User-Model. The user can edit the attributes (address, name, password, locale).
To be RESTful, I've created a resource for the User-Model
#routes.rb
resources :users, only: [:edit, :update]
The page where the user can update his attributes (/users/:id/edit) contains multiple forms (one for the general information like address, one for the locale-setting and one to change his password).
each of this three forms looks something like this:
= form_for(current_user, html: { class: 'fill-up' }) do |f|
.padded
= render 'shared/error_messages', object: current_user
= f.label :locale
.input
= f.select(:locale, [['Deutsch', 'de'], ['English, 'en']])
.form-actions
= f.button 'Submit', class: 'button'
The problem is, if I render the edit-action to display the error-messages for the current_user-object, these error-messages will be shown on all three forms.
What is the Rails best-practice to split the model-attributes in different forms and display the error-messages only on one specific form, rather than on all.
Upvotes: 0
Views: 371
Reputation: 1035
one solution for this is, lets say you have 2 forms 1) address 2) password
then in address edit form display only errors for address attribute for current_user like,
current_user.errors[:address] if current_user.errors.has_key?(:address )
and not
current_user.errors
similarly for password form you just display password errors
current_user.errors[:password] if current_user.errors.has_key?(:password )
this way you can avoid same errors in all forms.
Upvotes: 1