Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add a dropdown/select using RoR helper?

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.

like image 454
Tattat Avatar asked Jul 09 '10 14:07

Tattat


1 Answers

Ref select and options_for_select

<%= f.select :role, options_for_select([["Teacher", "t"], ["Student", "s"]]) %>
like image 62
Salil Avatar answered Sep 18 '22 14:09

Salil