Action records
# avo-2
w
I'm using an Action to modify records selected by the record selector. I thought these selected records would be in the :models field of the *def handle(*args) , am I wrong? I'm getting an empty array even when records have been selected.
l
Hello @white-piano-9142 ,you're right. Can I see your action configuration?
w
Sure.
Copy code
class MergeConnectionFiles < Avo::BaseAction
  self.name = "Merge connection files"

  def handle(**args)
    models, fields, current_user, resource = args.values_at(:models, :fields, :current_user, :resource)


    Rails.logger.info "parent: #{models.inspect} "
    redirect_to "/avo/resources/connection"
  end
end`
I've used the debugger to inspect the args, :models is an empty array. The log looks like:
07:14:30 web.1 | Started POST "/home/resources/connections/actions" for ::1 at 2023-08-06 07:14:30 -0500 07:14:30 web.1 | User Load (16.5ms) SELECT
users
.* FROM
users
WHERE
users
.
id
= 170 ORDER BY
users
.
id
ASC LIMIT 1 07:14:30 web.1 | Processing by Avo::ActionsController#handle as TURBO_STREAM 07:14:30 web.1 | Parameters: {"authenticity_token"=>"[FILTERED]", "action_id"=>"MergeConnectionFiles", "fields"=>{"avo_resource_ids"=>"", "avo_selected_query"=>""}, "button"=>"", "resource_name"=>"connections"} 07:14:30 web.1 | parent: [] 07:14:30 web.1 | Redirected to http://localhost:3000/avo/resources/connection
l
Hmm that's kinda strange, you're using the select all feature ir manually picking some records?
Btw you just created that action or its a persistent behavior? When changing action make sure to restart the server
Sorry for the dealy I was afk yesterday
w
No worries! AFK days are good, I'm sure this Discord is like a nother job!
I get this problem both ways, using select all or manually picking records. This action was originally built to merge selected records into a STI, but failed testing, the code I sent is what I scaled it back to for debugging. I've also moved the action to a way simpler model {id, name, weight} and I get the same results.
This morning I went into the browser console and I found this: "actions_picker_controller.js:44 Uncaught TypeError: Cannot read properties of null (reading 'loaded') at actions_picker_controller.js:44:34"

https://cdn.discordapp.com/attachments/1137547478204428350/1138092976539697172/Screenshot_2023-08-07_at_7.47.48_AM.png

This happens right after I invoke the action before the action modal window appears.
l
Hmm 🤔 you're using the action only on STI models?
Have you configured the model class on that resources?
w
The code above just logs what selected models came in on the action, it's always empty. I removed the origin STI logic during debugging and it seems like the selected models aren't passed into the action. The Browser error seems to imply a problem in the actions_picker -I'm not sure I'll dig that far. I've looked at selections being passed for other resources and it isn't happening there either.
I made a brand new action that prints the number of models and does and inspect on the :models arg.
Copy code
class TestModelArgs < Avo::BaseAction
  self.name = "Test model args"
  # self.visible = -> do
  #   true
  # end

  def handle(**args)
    models, fields, current_user, resource = args.values_at(:models, :fields, :current_user, :resource)

    Rails.logger.info "count: #{models.length} models: #{models.inspect} "
    succeed "Done"
  end
end`
It is always empty even if there were rows selected. Started POST "/home/resources/equipment/actions" for ::1 at 2023-08-07 17:54:57 -0500 17:54:57 web.1 | User Load (19.0ms) SELECT
users
.* FROM
users
WHERE
users
.
id
= 170 ORDER BY
users
.
id
ASC LIMIT 1 17:54:57 web.1 | Processing by Avo::ActionsController#handle as TURBO_STREAM 17:54:57 web.1 | Parameters: {"authenticity_token"=>"[FILTERED]", "action_id"=>"TestModelArgs", "fields"=>{"avo_resource_ids"=>"", "avo_selected_query"=>""}, "button"=>"", "resource_name"=>"equipment"} 17:54:57 web.1 | count: 0 models: [] 17:54:57 web.1 | Redirected to http://localhost:3000/home/resources/equipment/actions?action_id=TestModelArgs 17:54:57 web.1 | Completed 302 Found in 3ms (ActiveRecord: 0.0ms | Allocations: 2582) It seems like the issue is in actions_picker_controller.js I'm gonna abandon this path and find another way. Thanks for your time.
l
I can't reproduce it, do you have any monkey patches? Are you using Avo 2 or avo 3?
Can you create a little reproduction repository where we can experience the bug? Is hard to fix it if it doesn't happen on our side
w
We're on AVO 2. No monkey patches. Changed the base "avo/" root route to "home/" is all. I have another dev here who wants to take a swing at the Stimulus controller where the error occurs. Can we override those controllers? I can't do a meaningful repo, and I'd have to change too much for security. However, if you are saying this works as intended for you, that's good enough for me. We may do a clean install and work from there.
l
You«re running the action on the resource index / show or inside a tab through some association resource?
w
resource index
l
I watched your action's code snippet but can you confirm that the action is not configured with the option
self.standalone = true
w
Confirmed. It is not a standalone action.
l
Hmm, can you provide the resource's where the action is runing code? Also the model code?
w
Sure.
l
Thanks
Maybe i can reproduce from there
w
Copy code
class EquipmentResource < Avo::BaseResource
  self.title = :display_name
  self.includes = []
  # self.search_query = -> do
  #   scope.ransack(id_eq: params[:q], m: "or").result(distinct: false)
  # end

  field :id, as: :id
  # Fields generated from the model
  field :display_name, as: :text
  # add fields here
  action TestModelArgs
end
Copy code
# == Schema Information
#
# Table name: equipment
#
#  id             :integer          not null, primary key
#  display_name   :string(255)      not null
#  object_type_id :integer          default(34), not null
#
class Equipment < ApplicationRecord
  def self.options
    result = {}
    all.each do |el|
      result[el.display_name] = el.id
    end
    result
  end
end
we're on Ruby 3.1.2 Rails 7
l
That is all the code or you cut irelevant parts?
w
That's all the code, this is the simplest table with the same issue as the original model.
l
I reproduced it all without the
object_type_id
integer
and i get
Copy code
bash
web    | count: 2 models: [#<Equipment id: 2, display_name: "", created_at: "2023-08-08 14:18:16.521692000 +0000", updated_at: "2023-08-08 14:18:16.521692000 +0000">, #<Equipment id: 1, display_name: "", created_at: "2023-08-08 14:18:13.093917000 +0000", updated_at: "2023-08-08 14:18:13.093917000 +0000">]
w
I see! Let me remove the object_type_id from over here.
l
I think that is not the issue
you can try and tell me the output
but i think there is some configuration somewherew that makes this happening
the JS error still happening?
w
running it now
I'm getting a different error, I'm retesting
l
can you share the error please? maybe i get a clue
w
sure
stimulus.js:1658 Error connecting controller Error: Missing target element "resourceIds" for "action" controller at n.get (stimulus.js:1727:27) at n.connect (action_controller.js:8:10) at sle.connect (stimulus.js:1068:29) at ule.connectContextForScope (stimulus.js:1260:17) at gle.scopeConnected (stimulus.js:1575:20) at mle.elementMatchedValue (stimulus.js:1495:27) at lz.tokenMatched (stimulus.js:793:27) at sz.tokenMatched (stimulus.js:726:23) at stimulus.js:720:38 at Array.forEach () {identifier: 'action', controller: n, element: div.text-slate-800}
l
Hmm it seems that the js controller complain about missing parts that we have on our code
It would be easy to explain if you were overriding or monkey patching some files and changing the code, otherwise I have no idea what it can be
It's also a wierd behavior that the error changed like that 😕
w
I agree, I'm taking another pass through this branch
l
I would love to help you but without being able to reproduce it it becomes kinda hard
w
Ok, it seems like on the first clean run I get the original error. Each attempt after that I get this newer error.
l
When is the error trigger when you click on the action or when you run it?
w
When I click on the action
Do I need additional pundit policy methods for Actions?
l
Do you have a policy class settled for that resource ?
If you have you should include the
def act_on?
method
w
Yep, it's already in there. Just thinking through it.
Paul, I really appreciate your time today. I'll start a clean AVO branch, see it working there then compare back to our problem app.
l
Anytime, I'm sorry we couldn't fix it, that's a project specific problem and it's hard to debug without acces to the project or at least to a isolated reproduction repo. Let me know if you ever found out what's causing the issue
w
Will do. I already have a new AVO build running against the same DB and it's working. I now have to start adding back in the different gems.
l
That's "good news "
w
And fixed it. I'll narrow down the specific offender but the short list is: - an unclosed tag in the footer partial - application.js missing include for turbo-rails. - redundant gems for what was already included in Rails 7.
l
That's amazing news! Glad you managed to fix it!
w
Thanks for doing the sanity checks!