0

I'm using the Best In Place Gem to do inline edits on a table of Tasks that has a nested attribute for Storeorder, however when I try to edit a Storeorder attribute using the instructions provided in this post, I get a 204 No Content error thrown at me. I wonder if it has to do with the first transaction beginning before the 'Storeorder Load' happens? In all non-nested BIP updates, it does the UPDATE within the first "begin transaction" call, whereas here it's still loading the Storeorder. The parameters are 100% correct as far as I can tell. See code,

Started PUT "/tasks/3" for 104.200.151.54 at 2017-02-05 18:08:24 +0000
Processing by TasksController#update as JSON
  Parameters: {"task"=>{"storeorder_attributes"=>{"id"=>"3", "activity"=>"Shipped"}}, "authenticity_token"=>"D2c3ddoIC220rkPE5i7U+EGiwSrdCq7s8vdFY8VEQTaTMqetuBo8SJX9+Wabl+Bh6A6d49Pt/Omp4E/nq/udQA==", "id"=>"3"}
  User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ?  [["id", 1], ["LIMIT", 1]]
  Task Load (0.2ms)  SELECT  "tasks".* FROM "tasks" WHERE "tasks"."id" = ? LIMIT ?  [["id", 3], ["LIMIT", 1]]
  CACHE (0.0ms)  SELECT  "tasks".* FROM "tasks" WHERE "tasks"."id" = ? LIMIT ?  [["id", 3], ["LIMIT", 1]]
   (0.1ms)  begin transaction
  Storeorder Load (0.2ms)  SELECT  "storeorders".* FROM "storeorders" WHERE "storeorders"."task_id" = ? LIMIT ?  [["task_id", 3], ["LIMIT", 1]]
   (0.1ms)  commit transaction
   (0.1ms)  begin transaction
   (0.1ms)  commit transaction
Completed 204 No Content in 10ms (ActiveRecord: 1.0ms)

tasks_controller.rb -->

class TasksController < ApplicationController
  before_action :set_task, only: [:show, :edit, :update, :destroy]

  def update
    @task = Task.find(params[:id])
    respond_to do |format|
      if @task.update(task_params)
        format.html { redirect_to @task, notice: 'Task was successfully updated.' }
        format.json { respond_with_bip(@task) }
      else
        format.html { render :edit }
        format.json { respond_with_bip(@task) }
      end
    end
  end

  private
    def set_task
      @task = Task.find(params[:id])
    end

    def task_params
      params.require(:task).permit!
    end
end

task.rb -->

class Task < ApplicationRecord
  has_one :storeorder, :dependent => :destroy
  accepts_nested_attributes_for :storeorder, :reject_if => lambda { |a| a[:store_id].blank? }, :allow_destroy => true
end

storeorder.rb -->

class Storeorder < ApplicationRecord
    belongs_to :task
end

dashboard.html.erb -->

<td><%= best_in_place task.storeorder, :activity, 
        url: task_path(task.id), 
        param: "task[storeorder_attributes][id]=#{task.storeorder.id}&task[storeorder_attributes]", 
        as: :select, 
        collection: [["Pending Shipment", "Pending Shipment"], ["Shipped", "Shipped"], ["Cancelled", "Cancelled"], ["Pending Further Action", "Pending Further Action"]], %>
</td>

inner HTML code -->

<span 
  data-bip-type="select" 
  data-bip-attribute="activity" 
  data-bip-collection="[["Pending Shipment","Pending Shipment"],["Shipped","Shipped"],["Cancelled","Cancelled"],["Pending Further Action","Pending Further Action"]]" 
  data-bip-inner-class="form-control" 
  data-bip-object="task[storeorder_attributes][id]=3&task[storeorder_attributes]" 
  data-bip-original-content="Pending Shipment" 
  data-bip-skip-blur="false" 
  data-bip-url="/tasks/3" 
  data-bip-value="Shipped" 
  class="best_in_place form-control" 
  id="best_in_place_storeorder_3_activity">
  Shipped
</span>

I can't see what I could possibly be missing that causes this error. It's imperative that I'm allowed to do inline edits to keep the workflow consistent, otherwise I'm open to alternative suggestions since I know BIP doesn't have nested attribute editing within their scope by default.

Community
  • 1
  • 1
Tyler P
  • 63
  • 2
  • 7

1 Answers1

1

:reject_if => lambda { |a| a[:store_id].blank? } Don't see any store_id being passed in params.

Abhishek Kumar
  • 674
  • 1
  • 6
  • 15
  • Well I feel like an idiot now. I was adding and removing code all over the place trying to pinpoint the issue, and never once paid any attention to that reject clause. It is there for the New view where the Task/Store Order combo is initially created to ensure the Storeorder table is 100% blank if the task isn't set to 'Storeorder'. Removing that line fixes my issue in the Dashboard, but do you know any way that I can keep that reject_if clause to only apply to to the New Task view? Thank you very much for the help @Abhishek Kumar! – Tyler P Feb 06 '17 at 05:09
  • 1
    you can add `class Task < ApplicationRecord accepts_nested_attributes_for :storeorder, :reject_if => :method_name?, :allow_destroy => true def method_name(attributes) self.new_record? && attributes[:store_id].blank? end ` – Abhishek Kumar Feb 06 '17 at 07:30