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:
But I want it to be only like
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!
要做到这一点, pluck
了User
通过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.