简体   繁体   English

Rails:在下拉列表中列出记录

[英]Rails: List records in Dropdown

I've got some Users in my DB with a column called "role". 我的数据库中有一些“角色”列的用户。 The role contains either admin, manager or user as the value. 该角色包含admin,manager或user作为值。 Now I want to loop through this column and list only the respective role once and not all in a Dropdown. 现在,我想遍历此列,只列出一次各自的角色,而不是在下拉列表中全部列出。 The current dropdown lists something like: 当前下拉列表列出了类似的内容:

  • admin 管理员
  • user 用户
  • user 用户
  • user 用户
  • user 用户
  • manager 经理

But I want it to be only like 但我希望它就像

  • admin 管理员
  • user 用户
  • manager 经理

This is my current code: 这是我当前的代码:

    <%= f.label :role %>
    <%= f.collection_select(:role, User.all, :role, :role, {prompt:true},    {class: 'form-control'}) %>

Any Ideas? 有任何想法吗? Thank you guys! 感谢大伙们!

要做到这一点, pluckUser通过uniq的角色模型:

<%= f.select(:role, User.pluck(:role).uniq, {prompt:true}, {class: 'form-control'}) %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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