在我的Rails 4控制器的動作,我傳遞了以下PARAMS創建兩個對象:Rails中4控制器strong_params
{"user"=>{"email"=>"", "password"=>"[FILTERED]", "profile"=>{"birthday"=>nil, "gender"=>nil, "location"=>"", "name"=>""}}}
從這些PARAMS
現在我希望建立兩個對象:User
及其Profile
。
我試過下面的代碼的重複,但不能獲得通過strong_params
問題:
def create
user = User.new(user_params)
profile = user.build_profile(profile_params)
...
end
private
def user_params
params.require(:user).permit(:email, :password)
end
def profile_params
params[:user].require(:profile).permit(:name, :birthday, :gender, :location)
end
由於上面的代碼中拋出一個Unpermitted parameters: profile
錯誤,我想知道,如果profile_params
方法,甚至曾經讓擊中。它幾乎感覺我需要在user_params
profile
並處理該問題。我已經試過了,以及,我strong_params
方法更改爲:
def create
user = User.new(user_params)
profile_params = user_params.delete(:profile)
profile = user.build_profile(profile_params)
...
end
private
def user_params
params.require(:user).permit(:email, :password, profile: [:name, :birthday, :gender, :location])
end
,但我得到ActiveRecord::AssociationTypeMismatch - Profile(#70250830079180) expected, got ActionController::Parameters(#70250792292140):
任何人都可以闡明這一些輕?
啊,除了不知道。謝謝! – tvalent2