我正在浏览并清理一些表格,并意识到当用户点击编辑培训时,状态将恢复为阿拉斯加而不是当前所处的状态。现在代码在帮助器中看起来像这样:
module Admin::StatesHelper
STATES = [
['Alaska', 'AK'],
['Alabama', 'AL'],
['Arkansas', 'AR'],
['American Samoa', 'AS'],
['Arizona', 'AZ'],
['California', 'CA'],
['Colorado', 'CO'],
['Connecticut', 'CT'],
['District of Columbia', 'DC']
].freeze
def options_for_us_state(selected = nil, abbrev_only: true)
values = abbrev_only ? STATES.map { |s| s[1] } : STATES
options_for_select(values, selected)
end
end
然后以我们的形式:
<%= f.tb_select :state, options_for_us_state(params[:state]), prompt: 'Select State'%>
我可以创建状态,因为它是单独的模型,然后使用options_from_collection_for_select,但是我的方法不应该工作,或者我输入的东西是不能捕获所选的?
答案 0 :(得分:0)
这是一种愚蠢的形式。使用以下内容:
<%= f.tb_select :state, options_for_us_state(@location.state), prompt: 'Select State'%>