我有一个这样的:
<% 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 %>
在数据库中,role 是一个“Char”字段。我希望它不同于文本字段,用户可以选择“教师”、“学生”,如果用户选择“教师”,数据库将存储“T”,否则将存储“S”。我该怎么做?是不是需要我在数据库里加一张“用户角色”表,然后和用户建立关系?但是有必要这样做吗?谢谢你。
最佳答案
<%= f.select :role, options_for_select([["Teacher", "t"], ["Student", "s"]]) %>
关于ruby-on-rails - 如何使用 RoR 助手添加下拉菜单/选择?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3213830/