rails attribute changes in spite of not being in form

前端 未结 2 874
鱼传尺愫
鱼传尺愫 2021-01-16 07:01

I have rails app where users can assign tasks to each other. Every task has one assigner and one executor. By default the task creator(current_user) is always the assigner.

2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-16 07:56

    def task_params
      params.require(:task).permit(:executor_id, :name, :content, :deadline, :task_name_company).merge(assigner_id: current_user.id)
    end
    

    You are setting the assigner id to current_user every time you pass in the params.

    I would remove the merge, and just have strong params like this:

    params.require(:task).permit(:executor_id, :name, :content, :deadline, :task_name_company, :assigner_id)
    

    Then, in your create action, before you do:

    @task.save
    

    Add this:

    @task.assigner_id = current_user.id
    

    Then you can define your permissions however you choose (must be assigner, must be executor), but as long as you don't add anything crazy to update, it'll work fine.

提交回复
热议问题