【问题标题】:How can I add a dropdown/select using RoR helper?如何使用 RoR 助手添加下拉/选择?
【发布时间】:2010-07-09 14:58:11
【问题描述】:

我有这样的一封信:

<% 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 %>            

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

【问题讨论】:

标签: ruby-on-rails database


【解决方案1】:

参考selectoptions_for_select

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

【讨论】:

  • 我想您的意思可能是f.select 而不是f.select_tag,对吧? - 因为你有表单构建器实例f
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-25
  • 2020-02-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多