如何让数据库读取表单值,而不是选项号

时间:2013-05-09 19:27:42

标签: ruby-on-rails best-in-place

对于我的个人资料字段,例如种族。用户1选择了“亚洲人”作为他们的种族。在用户数据库中,它将显示所选种族的数字(例如亚洲= 1,荷兰= 2等,基于选项列表中的种族顺序),搜索数据库仅识别种族本身...而不是数字为了种族。因此,搜索正在尝试查找选择了亚洲人的用户,但从技术上讲,由于用户数据库使用的是数字而不是种族名称,因此没有。

我相信我的用户数据库正在显示数字,因为这是在best_in_place gem的表单中使用的。

show.html:

 <p>Ethnicity: <%= best_in_place @user, :ethnicity, nil: 'What is your ethnicity?', :type => :select, :collection => [[1, "Asian"], [2, "Biracial"], [3, "Indian"], [4, "Hispanic/Latin"], [5, "Middle Eastern"], [6, "Native American"], [7, "Pacific Islander"], [8, "White"], [9, "Other"]] %></p>

我尝试过:

collection: ["Asian", "Biracial", "Indian"]

collection: [ ["Asian", "Asian"], ["Biracial", "Biracial"], ["Indian", "Indian"]]

如果我做第一个例子,应该读作“亚洲人”的选项将改为“s”。

第二个示例没有显示任何选项,因此它只是一个空白的下拉框。

有谁知道如何将这些选项保存到用户数据库作为与数字相对的实际选项?

模式:

 create_table "searches", :force => true do |t|
    t.string   "gender"
    t.string   "age"
    t.string   "zip_code"
    t.string   "children"
    t.string   "religion"
    t.string   "ethnicity"
    t.datetime "created_at", :null => false
    t.datetime "updated_at", :null => false
  end

  create_table "users", :force => true do |t|
    t.string   "email"
    t.string   "password_digest"
    t.string   "zip_code"
    t.string   "birthday"
    t.string   "name"
    t.string   "username"
    t.string   "gender"
    t.string   "ethnicity"
    t.string   "sexuality"
    t.string   "career"
    t.string   "education"
    t.string   "religion"
    t.string   "politics"
    t.string   "children"
    t.string   "height"
    t.string   "user_smoke"
    t.string   "user_drink"
    t.string   "about_me"
    t.string   "inches"
    t.string   "feet"
    t.datetime "created_at",             :null => false
    t.datetime "updated_at",             :null => false
    t.string   "auth_token"
    t.string   "password_reset_token"
    t.datetime "password_reset_sent_at"
    t.boolean  "admin"
    t.string   "role"
    t.integer  "roles_mask"
    t.string   "age"
    t.string   "age_end"
  end

1 个答案:

答案 0 :(得分:0)

我想出了问题。最好的代码很好,我只需要修改搜索表单。

<%= f.select :ethnicity, [["Asian", "1"], ["Biracial", "2"], ["Indian", "3"], ["Hispanic/Latin", "4"], ["Middle Eastern", "5"], ["Native American", "6"], ["Pacific Islander", "7"], ["White", "8"], ["Other", "9"]], :include_blank => true %>

正如您所看到的,我添加了在种族名称后显示在我的数据库中的值。现在,这可以完美地进行搜索。