为什么 ActiveAdmin 呈现空字符串而不是关系的“to_s”方法?
Why is ActiveAdmin rendering empty strings instead of the `to_s` method of a relation?
我正在使用 ruby 2.7.1、rails 6.0.0 和最新的 ActiveAdmin。该应用程序是一个专注于推荐和赏金支付的技术招聘平台。
我有一个 BountyPayment 模型 belongs_to
三个其他模型,即这里是 app/models/bounty_payment.rb
文件:
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
包括:has_many :bounty_payments, inverse_of: :bounty_status, dependent: :destroy
placement.rb
包括:has_many :bounty_payments, inverse_of: :placement, dependent: :destroy
user.rb
包括:has_many :bounty_payments, inverse_of: :user, dependent: :destroy
我的应用程序中还有许多其他模型和关系,这个问题从未在其他任何地方出现过。
意外的行为是当我创建一个新的 BountyPayment 时,包括给它一个 bounty_status_id、placement_id 和 user_id,Placement 列在 ActiveAdmin 中到处都是空白。其他列不是空白。当我下载 CSV 时,我可以看到正确的位置,但我在 ActiveAdmin 中看不到它(如果我检查元素它是空白的,即 HTML 中的空字符串)。用于在 ActiveAdmin 中创建或编辑 BountyPayment 的下拉菜单中的 Placement 选项也是空白的。就好像 ActiveAdmin 用来在下拉菜单和页面中呈现展示位置的任何方法都返回一个空字符串或 nil 值。我不知道为什么它会那样做,并且不能在其他任何地方复制它。
以下是该行为的图片。
- BountyPayment table with empty string for Payment
- Details page with empty string
- Dropdown not showing string as an option despite showing the correct number of options from the Placement table
注意:我在 ActiveAdmin 上使用皮肤。
这是(我相信)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
我相信 Placement 资源也在 app/admin/placements.rb
的 ActiveAdmin 中正确注册,如下所示:
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
我在 Chrome、Firefox 和 Safari 中进行了测试,这三种浏览器的行为都相同。
我在rails控制台运行ActiveAdmin.application.namespaces[:admin].resources.keys
显示这里提到的所有资源确实已经注册所以我认为问题不是缺少资源。
这是 app/models/placement.rb
文件,包括我现在正在使用的(简单的)to_s
方法:
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
我很乐意提供更多信息。提前感谢您的任何想法!
ActiveAdmin 通过选择 object 响应的第一个方法来选择要为 object 显示的内容。
:display_name, :full_name, :name, :username, :login, :title, :email, :to_s
[来自 Index As Table]
Placement 有一个 title
方法,因此它正在使用该方法。大概您的展示位置有一个空白标题。
您可以通过定义显式 display_name
方法来解决这个问题。
我正在使用 ruby 2.7.1、rails 6.0.0 和最新的 ActiveAdmin。该应用程序是一个专注于推荐和赏金支付的技术招聘平台。
我有一个 BountyPayment 模型 belongs_to
三个其他模型,即这里是 app/models/bounty_payment.rb
文件:
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
包括:has_many :bounty_payments, inverse_of: :bounty_status, dependent: :destroy
placement.rb
包括:has_many :bounty_payments, inverse_of: :placement, dependent: :destroy
user.rb
包括:has_many :bounty_payments, inverse_of: :user, dependent: :destroy
我的应用程序中还有许多其他模型和关系,这个问题从未在其他任何地方出现过。
意外的行为是当我创建一个新的 BountyPayment 时,包括给它一个 bounty_status_id、placement_id 和 user_id,Placement 列在 ActiveAdmin 中到处都是空白。其他列不是空白。当我下载 CSV 时,我可以看到正确的位置,但我在 ActiveAdmin 中看不到它(如果我检查元素它是空白的,即 HTML 中的空字符串)。用于在 ActiveAdmin 中创建或编辑 BountyPayment 的下拉菜单中的 Placement 选项也是空白的。就好像 ActiveAdmin 用来在下拉菜单和页面中呈现展示位置的任何方法都返回一个空字符串或 nil 值。我不知道为什么它会那样做,并且不能在其他任何地方复制它。
以下是该行为的图片。
- BountyPayment table with empty string for Payment
- Details page with empty string
- Dropdown not showing string as an option despite showing the correct number of options from the Placement table
注意:我在 ActiveAdmin 上使用皮肤。
这是(我相信)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
我相信 Placement 资源也在 app/admin/placements.rb
的 ActiveAdmin 中正确注册,如下所示:
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
我在 Chrome、Firefox 和 Safari 中进行了测试,这三种浏览器的行为都相同。
我在rails控制台运行ActiveAdmin.application.namespaces[:admin].resources.keys
显示这里提到的所有资源确实已经注册所以我认为问题不是缺少资源。
这是 app/models/placement.rb
文件,包括我现在正在使用的(简单的)to_s
方法:
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
我很乐意提供更多信息。提前感谢您的任何想法!
ActiveAdmin 通过选择 object 响应的第一个方法来选择要为 object 显示的内容。
:display_name, :full_name, :name, :username, :login, :title, :email, :to_s
[来自 Index As Table]
Placement 有一个 title
方法,因此它正在使用该方法。大概您的展示位置有一个空白标题。
您可以通过定义显式 display_name
方法来解决这个问题。