class UsersController < ApplicationController
....
def update
if @user.update(edit_user_params)
flash[:success] = 'Profilo aggiornato correttamente'
redirect_to user_path(@user)
else
render action: 'edit'
end
end
....
private
def edit_user_params
params.require(:user).permit(:email, information_attributes: [:name, :surname, :date_of_birth, :address, :city, :country, :post_code, :phone])
end
end
As all you can see I have nested forms.
The Informatation model is made by the following column:
- name
- surname
- date_of_birth
- address
- city
- country
- post_code
- phone
- credit
In the nested forms there is not an input field for the credit, because to update the credit I created another flow.
The problem is the following, when rails does @user.update(edit_user_params) it goes to overwrite all the columns of the Information row related to the particular user, that is, if the credit was 15$ after the update it becomes 0. What does that happen? Shouldn't the update method update only the columns pointed in ()?
In addiction, because in the Information.rb I have the following validate:
validates :credit, numericality: { greater_than_or_equal_to: 0.01 }
I always get an error because the credit field results equals to 0.0