0

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

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

显示.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
4

1 回答 1

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

如您所见,我在种族名称之后添加了在我的数据库中显示的值。现在,这可以完美地拉起搜索量。

于 2013-05-10T17:26:46.107 回答