【发布时间】:2012-06-05 08:46:47
【问题描述】:
我正在尝试在 Rails 3.2 应用程序中实现 select2。
具体来说,我想使用模板来显示带有国家标志+国家名称的国家选择字段。
我有一个 Country 模型和 User 模型。 My Country 模型具有:code 属性。我正在使用 CSS 精灵来显示标志,基于 :code 属性。
<img class="flag flag-#{country.code}">
在我的用户表单中
<%= f.collection_select :country_id, Country.order(:name), :id, :name, include_blank: true %>
在 user.js.coffee.erb 我有
jQuery ->
format = (country) ->
"<img class='flag flag-#{country.code}' src='#'/>" + country.name
$('#prerep_country_id').select2
placeholder: "Select a country",
allowClear: true,
formatResult: format,
formatSelection: format
我无法将它们结合在一起(可能是我不断学习资产管道和 js.erb 工作原理的一部分)。
目前,选择字段是使用 select2 呈现的,但仅包含国家/地区列表。没有格式。
如何将每个国家/地区传递给format = (country) 函数,以便使用标志对其进行格式化?
感谢您的任何指点。
【问题讨论】:
标签: jquery ruby-on-rails coffeescript jquery-select2