【问题标题】:Passing current user parameters into nested model将当前用户参数传递给嵌套模型
【发布时间】:2019-01-06 11:52:38
【问题描述】:

每当我更新提交模型上的参数时,我都会尝试将设计用户 ID 从我的代理模型 (current_agent.id) 传递到我的嵌套代理活动模型中。我将如何实现这一目标?

目前我在提交控制器上的代码(不抛出错误但不执行任何操作):

@submission.agent_activities.first.agent_id = current_agent.id

respond_to do |format|
  if @submission.update(submission_params)
    format.html { redirect_to @submission, notice: 'Submission was successfully updated.' }
    format.json { render :show, status: :ok, location: @submission }
  else
    format.html { render :edit }
    format.json { render json: @submission.errors, status: :unprocessable_entity }
  end
end

【问题讨论】:

    标签: ruby-on-rails database ruby-on-rails-5 parameter-passing nested-attributes


    【解决方案1】:

    是的,您只是分配 agent_id,但不要保存它。应该是:

    @submission.agent_activities.first.update(agent_id: current_agent.id)
    

    这一行应该在if @submission.update(submission_params)之后

    【讨论】:

    • @WernerAdewole,如果它适合您,请您接受答案
    猜你喜欢
    • 1970-01-01
    • 2011-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多