[英]How to add a conditional to show in ActiveAdmin
我有一個任務。 顯示不同的字段取決於我的對象參數。
例如,對於我的表單,如果當前對象的 message_type = 'custom' 我將顯示一個輸入,如果不是 - 另一個。
這是一個現在有效的代碼:
form do |f| if f.object.new_record? || f.object.message_type == 'custom' f.inputs do f.input :custom_topic f.input :custom_content, as: :text end f.actions end end
但是為了表演,我不知道如何檢查它。 我現在所擁有的:
show do attributes_table do if :message_type == 'custom' row :message_type row(:topic) { |object| object.notification_data['topic'] } row(:content) { |object| object.notification_data['content'] } else row :message_type row :notification_data end end end
當我運行調試器時,它會顯示我
message_type={符號}
我同意)
但是如何檢查當前對象的 message_type 的值呢?
我的所有代碼如下:
ActiveAdmin.register Notification do belongs_to :case, finder: :find_by_slug permit_params :message_type, :custom_topic, :custom_content, :notification_data form do |f| if f.object.new_record? || f.object.message_type == 'custom' f.inputs do f.input :custom_topic f.input :custom_content, as: :text end f.actions end end show do attributes_table do if :message_type == 'custom' row :message_type row(:topic) { |object| object.notification_data['topic'] } row(:content) { |object| object.notification_data['content'] } else row :message_type row :notification_data end end end config.filters = false end
在show
塊中,當前資源可用作通常以您管理的模型命名的方法。 在這種特殊情況下,它可能是notification
,因此以下可能有效:
show do
attributes_table do
if notification.message_type == 'custom'
row :message_type
row(:topic) { |object| object.notification_data['topic'] }
row(:content) { |object| object.notification_data['content'] }
else
row :message_type
row :notification_data
end
end
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.