如何使用RoR助手添加组合框?

时间:2022-11-21 00:16:34

I have a from like this:

我有一个这样的:

<% form_for(@user) do |f| %>
  <%= f.error_messages %>

  <p>
    <%= f.label :username %><br />
    <%= f.text_field :username %>
  </p>
  <p>
    <%= f.label :email %><br />
    <%= f.text_field :email %>
  </p>
  <p>
    <%= f.label :password %><br />
    <%= f.password_field :password %>
  </p>             
 <p>
    <%= f.label :password_confirmation %><br />
    <%= f.password_field :password_confirmation %>
  </p>
    <p>
        <%= f.label :role %> <br/>
        <%= f.text_field :role%>
    </p>
  <p>
    <%= f.submit 'Create' %>
  </p>
<% end %>            

In the database, role is a "Char" field. I want it different from a textfield, the user can select "Teacher", "Student", if the user select "Teacher", the database will store "T", otherwise, it will store "S". How can I do so? It is necessary for me to add a "User role" table in the database, and then make a relationship with the user? But it is necessary to do it this way? thank u.

在数据库中,角色是“Char”字段。我希望它与文本字段不同,用户可以选择“教师”,“学生”,如果用户选择“教师”,数据库将存储“T”,否则,它将存储“S”。我怎么能这样做?我有必要在数据库中添加“用户角色”表,然后与用户建立关系吗?但有必要这样做吗?谢谢你。

1 个解决方案

#1


17  

Ref select and options_for_select

参考选择和options_for_select

<%= f.select :role, options_for_select([["Teacher", "t"], ["Student", "s"]]) %>

#1


17  

Ref select and options_for_select

参考选择和options_for_select

<%= f.select :role, options_for_select([["Teacher", "t"], ["Student", "s"]]) %>