如果问题不够明确,我会保存,例如,在带有ID的数据库中说明。我可以返回该ID但是我很难将其转换为州名,例如“California”。
初始表格代码:
<%= f.select :user_state, options_for_select(states, f.object.user_state), {}, { :class => 'form-control' } %>
California是ID 5,正在使用以下代码正确保存和输出:
<%= current_user.user_state %>
但是,我需要它来显示“California”而不是“5”。
更新: 模特对此没有任何意义......
用户架构:
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "agreement_termspp", default: false
t.string "full_name"
t.string "phone_number"
t.text "extra_details"
t.string "user_address"
t.string "user_city"
t.string "user_state"
t.string "user_zipcode"
t.index ["email"], name: "index_users_on_email", unique: true, using: :btree
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
end
更新2:我有一个users_helper.rb文件,其中包含以下内容:
module UsersHelper
def states
[
["Select A State", "na1"],
["----", "na2"],
["Alabama", "1"],
["Alaska", "2"],
["Arizona", "3"],
["Arkansas", "4"],
["California", "5"],
["Colorado", "6"],
["Connecticut", "7"],
["Delaware", "8"],
["District Of Columbia", "9"],
["Florida", "10"],
["Georgia", "11"],
["Hawaii", "12"],
["Idaho", "13"],
["Illinois", "14"],
["Indiana", "15"],
["Iowa", "16"],
["Kansas", "17"],
["Kentucky", "18"],
["Louisiana", "19"],
["Maine", "20"],
["Maryland", "21"],
["Massachusetts", "22"],
["Michigan", "23"],
["Minnesota", "24"],
["Mississippi", "25"],
["Missouri", "26"],
["Montana", "27"],
["Nebraska", "28"],
["Nevada", "29"],
["New Hampshire", "30"],
["New Jersey", "31"],
["New Mexico", "32"],
["New York", "33"],
["North Carolina", "34"],
["North Dakota", "35"],
["Ohio", "36"],
["Oklahoma", "37"],
["Oregon", "38"],
["Pennsylvania", "39"],
["Rhode Island", "40"],
["South Carolina", "41"],
["South Dakota", "42"],
["Tennessee", "43"],
["Texas", "44"],
["Utah", "45"],
["Vermont", "46"],
["Virginia", "47"],
["Washington", "48"],
["West Virginia", "49"],
["Wisconsin", "50"],
["Wyoming", "51"],
["---- ", "na3"],
["American Samoa", "100"],
["Guam", "101"],
["Northern Mariana Islands", "102"],
["Puerto Rico", "103"],
["United States Minor Outlying Islands", "104"],
["Virgin Islands", "105"]
]
end
答案 0 :(得分:1)
在user.rb
中,您可以定义一个方法
def user_state_name
State.find_by_id(user_state).try(:name)
end
然后你可以把它称为
current_user.user_state_name
<强>更新强>
在助手中定义另一个方法,将其转换为哈希值,并返回id
的状态名称def state_name(id)
states.to_h.invert[id.to_s]
end
在您看来,
state_name(current_user.user_state)
虽然一个小小的建议,因为它们是常数,让它们成为......
STATES = [ #whole state array
].freeze
STATES_HASH = STATES.to_h.invert.freeze
def states
STATES
end
def state_name(id)
STATES_HASH[id.to_s]
end
答案 1 :(得分:1)
在状态辅助数组中找到所需状态的位置,如:
idx = states.index { |state| state.second == current_user.user_state }
然后抓住它
states[idx]
可以放在一行中,如:
states[states.index { |state| state.second == current_user.user_state }]