简体   繁体   中英

Why is ActiveAdmin rendering empty strings instead of the `to_s` method of a relation?

I am using ruby 2.7.1, rails 6.0.0, and the most recent ActiveAdmin. The app is a tech recruiting platform focused on referrals and bounty payments.

I have a BountyPayment model that belongs_to three other models, ie, here is the app/models/bounty_payment.rb file:

class BountyPayment < ActiveRecord::Base
  belongs_to :bounty_status, inverse_of: :bounty_payments
  belongs_to :placement, inverse_of: :bounty_payments
  belongs_to :user, inverse_of: :bounty_payments

  validates :amount_cents, numericality: { only_integer: true, greater_than_or_equal_to: 0, less_than_or_equal_to: 100000000, message: 'invalid numerical input; accepted range: 0-100000000' }

  def to_s
   [self.user, self.bounty_amount, self.bounty_status, self.placement].join(' - ')
  end
end

bounty_status.rb includes: has_many:bounty_payments, inverse_of: :bounty_status, dependent: :destroy

placement.rb includes: has_many:bounty_payments, inverse_of: :placement, dependent: :destroy

user.rb includes: has_many:bounty_payments, inverse_of: :user, dependent: :destroy

I have many other models and relations in the app and this problem never arises anywhere else.

The unexpected behavior is that when I create a new BountyPayment, including giving it a bounty_status_id, placement_id, and user_id, the Placement column appears blank everywhere in ActiveAdmin. The other columns are not blank. When I download CSV, I can see the correct Placement is there, but I can't see it in ActiveAdmin (if I inspect the element it is blank, ie, empty string in the HTML). The Placement options are also blank in the dropdown menus to create or edit the BountyPayment in ActiveAdmin. It is as if whatever method used by ActiveAdmin to render the Placements in dropdown menus and pages is returning an empty string or nil value. I don't know why it would do that, and can't replicate it anywhere else.

Below are images of the behavior.

Note: I am using a skin on ActiveAdmin.

Here is (I believe) the relevant part of the admin file for BountyPayments ( app/admin/bounty_payment.rb ):

ActiveAdmin.register BountyPayment do
  permit_params do
    permitted = :list, :of, :attributes, :on, :model
    if current_admin_user && ['create', 'update', 'destroy'].include?(params[:action])
      permitted << :id
      permitted << :bounty_status_id
      permitted << :placement_id
      permitted << :user_id
      permitted << :amount_cents
    end
    permitted
  end

I believe the Placement resource is also registered correctly in ActiveAdmin in app/admin/placements.rb as shown here:

ActiveAdmin.register Placement do
  permit_params do
    permitted = :list, :of, :attributes, :on, :model
    if current_admin_user && ['create', 'update', 'destroy'].include?(params[:action])
      permitted << :id
      permitted << :potential_interview_id
      permitted << :placement_status_id
      permitted << :title
      permitted << :notes
      permitted << :salary_cents
      permitted << :days_since_start
      permitted << :at_risk_for_refund
      permitted << :full_time_offer
      permitted << :remote_position
      permitted << :start_date
      permitted << :end_date
      permitted << :bounty_payments
    end
    permitted
  end

I tested in Chrome, Firefox, and Safari and the behavior was the same in all three browsers.

I ran ActiveAdmin.application.namespaces[:admin].resources.keys in rails console and it shows that all the resources mentioned here are indeed registered so I think the issue is not a missing resource.

Here is the app/models/placement.rb file including the (trivial) to_s method I am using right now:

class Placement < ActiveRecord::Base
  belongs_to :potential_interview, inverse_of: :placements
  belongs_to :placement_status, inverse_of: :placements

  has_many :bounty_payments, inverse_of: :placement, dependent: :destroy

  validates :title, length: { in: 1..100, message: 'title length must be between 1 and 100 inclusive' }, allow_blank: true
  validates :notes, length: { in: 1..1000, message: 'notes length must be between 1 and 1000 inclusive' }, allow_blank: true

  validates :salary_cents, numericality: { only_integer: true, greater_than_or_equal_to: 0, less_than_or_equal_to: 1000000000, message: 'invalid numerical input; accepted range: 0-1000000000' }, allow_blank: true
  validates :days_since_start, numericality: { only_integer: true, greater_than_or_equal_to: 0, less_than_or_equal_to: 100000, message: 'invalid numerical input; accepted range: 0-100000' }

  def to_s
   'hi'
  end
end

I am happy to provide additional information. Thanks in advance for any ideas!

ActiveAdmin chooses what to display for an object by picking the first of these methods which the object responds to.

:display_name, :full_name, :name, :username, :login, :title, :email, :to_s

[From Index As Table ]

Placement has a title method, so it is using that. Presumably your Placements have a blank title.

You can get around this by defining an explicit display_name method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM