Gem ActiveAdmin 中关联库调用

bluesky0318 · June 29, 2019 · Last by bluesky0318 replied at July 01, 2019 · 5888 hits

最近用 Activeadmin 定制后台,先做了两个 Resource,A 以及 B,其中 B belong_to A,然后将 A,B 资源加入 Activeadmin,做了一些假的资料分别存入 A,B 库,登录后台后,进入 A 选项,本来想将 B 库中的一些列举项显示在 A 库某属性下拉选项中,却发现引用 B 库字段资源却没有列举,直接列举的 B 库类对象地址。 我用的都是默认产生的资源,没有额外重新产生表单啥的,想问一下这种情况怎么修订啊? 可能说的不是太清楚,比如 A 库是一个学生库,这里面就有一个性别选项,然后 B 库就包含男,女两个选项,用 activeadmin 加入 A.B 资源后,想显示 A 资源对象时,性别那一行为下拉样式,这个内容又引用自 B 库,为男女两种模式,如何用 activeadmin 默认方式处理这个问题呢?一定要重新定义 index 表单么?

display_name是个好东西

Reply to pynix

display_name 这玩意怎么用啊,我看了一些 stackflow 里面的案例,似乎没有详细的说明用途。谢谢

还是重写了一把 index 实现了,先玩着吧,后面有精力再回头看看,谢谢

index do column :id column :osdescribe column :osorder column :updated_at column :categoryproject do |osplatform| Categoryproject.find(osplatform.categoryproject_id).categoryprojectstring end actions end

bluesky0318 closed this topic. 30 Jun 20:16
bluesky0318 reopened this topic. 30 Jun 22:14

看了 youtobe 上一位大牛的做法,发现他可以很轻松的实现这种模式,还是需要继续研究一下,重新开启一下

就是为 model 定义一个 display_name 方法,里面返回你想展示的内容。。。

Reply to pynix

谢谢,我再查查资料,没用过这个方法

Reply to pynix

yes, you are right @pynix , i set the method with def display_name categoryprojectstring #the attribute on the has_many model
end And now, the column work fine. thanks again.

More detail solution as following: Active Admin makes educated guesses when displaying objects.

This is the list of methods it tries calling in order

display_name full_name name username login title email to_s So, it works with categories since Category has :name attribute.

For labels you can do the following:

class Label < ActiveRecord::Base def display_name label_name end end You can also configure displayed names in ActiveAdmin initializer:

ActiveAdmin.setup do |config|
config.display_name_methods = [:display_name, :full_name, :name, ..., :to_s] end

bluesky0318 closed this topic. 09 Jul 11:26
You need to Sign in before reply, if you don't have an account, please Sign up first.