如何创建选择字段?

时间:2015-06-17 19:52:07

标签: ruby-on-rails ruby-on-rails-4

如何创建选择字段?

请帮助解决问题。

有表性别:

CREATE TABLE "genders" ("id"
    INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "gender_name"
    varchar, "created_at"
    datetime NOT NULL, "updated_at"
    datetime NOT NULL)

有一个表用户:

CREATE TABLE "users" ("id"
    INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name"
    varchar, "email"
    varchar, "password_digest"
    varchar, "remember_token"
    varchar, "phone"
    varchar, "skype"
    varchar, "info"
    varchar, "gender_id"
    integer DEFAULT 1, "created_at"
    datetime NOT NULL, "updated_at"
    datetime NOT NULL, "diary_name"
    varchar, "views_diary"
    integer DEFAULT 0, "admin"
    boolean DEFAULT 'f')

在模特'用户':

GENDER_TYPES = [['Lisbon', 1], ['Madrid', 2]]

在视图' new.html.erb':

<%= form_for(@user) do  |f| %>
  <div>
    <%= f.label :name %>
    <%= f.text_field  :name %>
  </div>

  <div>
    <%= f.label :gender  %>
    <%= f.select :gender,  User::GENDER_TYPES %>
  </div>  
<% end %>

但它不起作用。问题是,表单提交值未记录在&users; users.gender_id&#39;

之后

1 个答案:

答案 0 :(得分:1)

您的选择应该是设置gender_id,而不是gender

<%= f.select :gender_id,  User::GENDER_TYPES %>