出於某種原因,當我提交表單並更新我的用戶配置文件記錄時,我的用戶配置文件記錄因某種原因而更新爲零。rails 5強參數更新記錄集其他列爲零
假設User.last.user_profile有一個屬性「linkedin_url」,它被設置爲「www.yahoo.com」。提交表單後,User:電話號碼和UserProfile:work_address:work_zipcode會被更新,但「linkedin_url」會被設置爲零。 IDK爲什麼!
class UserProfilesController < ApplicationController
def update
@user = User.last
if @user.update(user_profile_params)
respond_to do |format|
format.html {
redirect_to user_profile_path(@user)
}
format.json {
render :show, status: :ok, location: @preference
}
end
end
end
def user_profile_params
params.require(:user).permit(:phone_number, user_profile_attributes: [:work_address, :work_zipcode])
end
形式
= form_for @user, url: user_profile_path(@user), method: :put do |f|
= f.fields_for :user_profile do |ff|
= ff.label :work_address
%br
= ff.text_field :work_address
%br
= ff.label :work_zipcode
%br
= ff.text_field :work_zipcode
%br
= f.label :phone_number
%br
= f.text_field :phone_number
%br
= f.submit "Submit", class: "btn"
只需將linkedin_url添加到接受的屬性params.require(:user).permit(:phone_number,user_profile_attributes:[:work_address,:work_zipcode,:linkedin_url]) –