我有一个错误,该错误不允许我保存关联。它只保存在Heroku托管的生产环境中。运行下面的代码后,该对象将用organization_id = nil
代码非常简单:
def create
@onboarding = Onboarding.new(onboarding_params)
if @onboarding.save
redirect_to 'something'
else
render action: :new
end
end
def onboarding_params
params.require(:onboarding).permit(:name, :position, :organization_id)
end
传入的参数如下所示:
params={"utf8"=>"✓", "authenticity_token"=>"[FILTERED]", "onboarding"=>{"name"=>"new-test", "organization_id"=>"1"}, "commit"=>"Create Onboarding", "controller"=>"admin/onboarding", "action"=>"create"}
ID为1
为100%的组织已存在。我也在Heroku托管的暂存环境中对此进行了测试,并且工作正常。有什么想法吗?
答案 0 :(得分:0)
这是因为id 1是一个数字,但在params中是一个字符串,您将需要执行onboarding_params[:organization_id].to_i
。所以你会有类似的东西
def create
@onboarding = Onboarding.new(
name: onboarding_params[:name],
position: onboarding_params[:position],
organization_id: onboarding_params[:organization_id].to_i
)
if @onboarding.save
redirect_to 'something'
else
render action: :new
end
end
def onboarding_params
params.require(:onboarding).permit(:name, :position, :organization_id)
end