Rails 5应用程序仅在生产环境中不保存某些属性/关联

时间:2019-05-23 13:36:47

标签: ruby-on-rails ruby ruby-on-rails-5

我有一个错误,该错误不允许我保存关联。它只保存在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托管的暂存环境中对此进行了测试,并且工作正常。有什么想法吗?

1 个答案:

答案 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