【问题标题】:How to get database to read form value, not option number如何让数据库读取表单值,而不是选项号
【发布时间】:2013-05-09 19:27:41
【问题描述】:

对于我的个人资料字段,例如种族。用户 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

【问题讨论】:

  • 你是如何在数据库中存储种族的?什么是列类型?你能发布你的 db/schema.rb 的相关部分吗?
  • 我添加了架构。它们都是字符串并存储为名称。它正在从我在 show.html 上的表单中获取数字。但我必须使用最好的宝石。

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


【解决方案1】:

我发现了问题所在。最好的代码很好,我只需要修改搜索表单。

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

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

【讨论】:

    猜你喜欢
    • 2015-02-10
    • 1970-01-01
    • 1970-01-01
    • 2021-07-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多