views:

27

answers:

1

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.

+2  A: 

Ref select and options_for_select

<%= f.select :role, options_for_select([["Teacher", "t"], ["Student", "s"]]) %>
Salil
I think you probably meant `f.select` rather than `f.select_tag`, right? - because you have the form builder instance `f`.
bjg
@bjg:- yea realise that later i edit my answer. anyway Thanx
Salil