I have a rails application where users can assign tasks to each other. Each task has one assignor and one executor. By default, the task creator (current_user) is always the assignor. Both the nominee and the executor can edit the same task.
My problem is as follows. Let's say user.id = 2 is the assigner and user.id = 1 is the executor. If the assigner (id = 2) edits the task later, everything works fine, but if the executor (id = 1) edits it, then he becomes the recipient, so task.executor_id = 1 and task.assigner_id = 1. There is no option for designator changes in a new / editable form. When the creator creates it, it is the default destination as the current user, and when sby tries to edit it, it cannot change the assignor. What causes this problem?
This is strange, since it worked well before, and I'm sure I had this problem, because I changed the use of the _task.html.erb part with <% = render @ tasks%> instead of going with the usual old @tasks .each do | task | version. At first I thought it happened because I made some mistakes with the new version of AJAXified, but after git reset --hard it looks like something else.
def task_params params.require(:task).permit(:executor_id, :name, :content, :deadline, :task_name_company) .merge(assigner_id: current_user.id) end class Task < ActiveRecord::Base belongs_to :assigner, class_name: "User" belongs_to :executor, class_name: "User"
_task.html.erb
<% if current_user.id == task.assigner.id %> <tr style="background: #eaeaea" id="task_<%= task.id %>"> <td class="col-md-3"> <%= link_to user_path(id: task.executor.id) do %> <%= task.executor.profile.first_name %> <%= task.executor.profile.last_name%> / <%= task.executor.profile.company %> <% end %> </td> <% else %> <tr id="task_<%= task.id %>"> <td class = "col-md-3"> <%= link_to user_path(id: task.assigner.id) do %> <%= task.assigner.profile.first_name %> <%= task.assigner.profile.last_name%> / <%= task.assigner.profile.company %> <% end %> </td> <% end %> <td class="cold-md-4"> <%= link_to user_task_path(id: task.id) do %> <%= task.content %> <% end %> </td> <td class="col-md-3"> <%= task.deadline %> </td> <td class="col-md-2" style="padding:0px"> <% if current_user.id == task.assigner.id %> <table class="table table-bordered inside-table" style="background:#eaeaea"> <% else %> <table class="table table-bordered inside-table"> <% end %> <tr> <td class="col-md-4" style="border-top:0px;border-bottom:0px;border-left:0px"> <% if task.completed_at == nil %> <%= link_to complete_user_task_path(id: task.id), action: :complete, remote: true, method: :patch do %> <i class="fa fa-check"></i> <% end %> <% else %> <%= link_to uncomplete_user_task_path(id: task.id), action: :uncomplete, remote: true, method: :patch do %> <i class="fa fa-check"></i> <% end %> <% end %> </td> <td class="col-md-4" style="border-top:0px;border-bottom:0px;"> <%= link_to edit_user_task_path(id: task.id), type: "button" do %> <i class="fa fa-pencil"></i> <% end %> </td> <td class="col-md-4" style="border-top:0px;border-bottom:0px;border-right:0px"> <%= link_to user_task_path(id: task.id), method: :delete, data: { confirm: "Are you sure?" }, remote: true do %> <i class="fa fa-trash"></i> <% end %> </td> </tr> </table> </td> </tr>
_form.html.erb
<%= form_for ([@user, @task]) do |f| %> <%= render 'layouts/error_messages', object: f.object %> <div class="field form-group"> <%= f.label :Name_or_Company %> <%= f.text_field :task_name_company, data: {autocomplete_source: user_tasknamecompanies_path}, class: "form-control task_name_company" %> </div> <div class="field form-group"> <%= f.label :content %> <%= f.text_area :content, class: "form-control" %> </div> <div class="field form-group"> <%= f.label :deadline %> <%= f.date_select :deadline, class: "form-control" %> </div> <div class="actions"> <%= f.submit "Create Task", class: 'btn btn-primary', "data-sid" => current_user.id, "data-rip" => :executor_id %> </div> <% end %>
UPDATE: The problem is solved based on the answers of Rich Peck and miler350. I have done the following:
before_action :set_assigner, only: :create private def set_assigner @task.assigner.id = current_user.id end