简体   繁体   中英

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. 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'}) %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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