Rails attribute changes even though they are not in shape

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 
+1
ruby-on-rails forms attributes
Oct 26 '15 at 23:41
source share
2 answers
 def task_params params.require(:task).permit(:executor_id, :name, :content, :deadline, :task_name_company).merge(assigner_id: current_user.id) end 

You set the destination identifier current_user every time you pass parameters.

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

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

Then, in your create action, before you execute:

 @task.save 

Add this:

 @task.assigner_id = current_user.id 

Then you can determine your permissions, however you choose (must be an appointee, must be an executor), but until you add anything crazy to update, it will work fine.

+3
Oct 27 '15 at 0:26
source share
β€” -

By default, the task creator (current_user) is always the assigner

It doesn’t matter - someone is an appointor, and someone is an executor.




miler350 correct - your parameters constantly set your assigner_id: current_user.id (looks like one of my suggestions).

The fix is ​​to remove .merge from your parameters and set it in your controller action (according to the answer of miler350 and the like):

 #app/controllers/tasks_controller.rb class TasksController < ApplicationController def new @task = Task.new end def create @task = Task.new task_params @task.assigner = current_user @task.save end def edit @task = Task.find params[:id] end def update @task = Task.find params[:id] @task.update task_params end private def task_params params.require(:task).permit(:executor_id, :name, :content, :deadline, :task_name_company, :assigner_id) end end 

By allowing users to "edit" assignment , you do not have to have different assigner / executor defined each time.

+1
Oct 27 '15 at 13:47
source share



All Articles