将当前用户参数传递到嵌套模型中

时间:2019-01-06 11:52:38

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

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

当前,我在“提交”控制器上的代码(不会引发错误,但是什么也没做):

$validation = Validator::make($request->all(), [
    'profile_photo'=> 'required|image|mimies:jpeg,png,jpg,gif|max:2048'
]);
if ($validation->passes()) {
    //$image = $request->file('profile_photo');
    $new_name = time().'.'.$request->image->getClientOriginalExtension();
    $request->image->move(public_path("photo"),$new_name);
    return response()->json([
        'message' => 'Image uploaded successfully'
    ]);
} else {
    return response()->json([
        'message' => $validation->errors()->all(),
        'profile_photo' => '',
        'class_name' => 'danger'
    ]);
}

1 个答案:

答案 0 :(得分:1)

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

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

此行应在if @submission.update(submission_params)之后