Gem ActiveAdmin 中关联库调用

bluesky0318 · 2019年06月29日 · 最后由 bluesky0318 回复于 2019年07月01日 · 5886 次阅读

最近用 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是个好东西

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 关闭了讨论。 06月30日 20:16
bluesky0318 重新开启了讨论。 06月30日 22:14

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

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

pynix 回复

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

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 关闭了讨论。 07月09日 11:26
需要 登录 后方可回复, 如果你还没有账号请 注册新账号