Select enum from form to set role

前端 未结 5 1356
醉酒成梦
醉酒成梦 2021-02-01 22:38

Ruby on Rails 4.1

I am using Devise with enum role. It currently sets a defualt role when the User is created. I want to add a field to the form that creates Users to se

5条回答
  •  生来不讨喜
    2021-02-01 23:32

    Since you are using Rails 4 or higher, enums are even less complicated.

    Given the following enum:

    enum role: {
      admin: 1
    }
    

    Enums expect the HTML option attribute value to be the enum key:

    Knowing this, you can pass in the enum keys.

    <%= f.select :role, User.roles.keys, {}, class: 'user-roles-select' %>
    

    Then using CSS you can modify the appearance.

    .user-roles-select option {
      text-transform: capitalize;
    }
    

提交回复
热议问题