我的用户是has_many服务:
class User < ActiveRecord::Base
has_many :services
accepts_nested_attributes_for :services, :reject_if => lambda { |s| s[:name].blank? }, :allow_destroy => true
end
这是我的控制器动作(设计)
def new
build_resource({})
5.times { resource.services.build }
...
end
def create
build_resource(sign_up_params)
resource.services.build(sign_up_params[:services_attributes])
...
end
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) do |u|
u.permit(:email, :password, :password_confirmation,
services_attributes: [:name])
end
end
当我提交表单时,这里是相关的params hash:
{...,
"services_attributes"=>{
"0"=>{"name"=>"Test"},
"1"=>{"name"=>""},
"2"=>{"name"=>""},
"3"=>{"name"=>""},
...}
给我以下错误:
unknown attribute: 0
在这种情况下,我不知道如何使用强参数保存多个对象。我的想法是做这样的事情:
在我的创建方法中:
resource.services.build(sign_up_params[:services_attributes][:id])
它可以保存物品,但我对此感觉不舒服......谢谢你的解释!
答案 0 :(得分:0)
应该只是:
def create
build_resource(sign_up_params)
...
end