ruby-on-rails - 如何使用 rails gem best_in_place 在选择字段中显示当前值?
问题描述
我正在使用 rails gem best_in_place。
在选择字段中,它不显示当前值,而是显示类似-
.
此外,如果我选择其他数据,日期字段不会更改日期。
<% @user.each do |record| %>
<tr>
<td>
<%= best_in_place record, :name, :as => :input %>
</td>
<td>
<%= best_in_place record, :gender, :as => :select, collection: (MUser.pluck(:id, :gender)) %>
</td>
<%= best_in_place record, :dob, :as => :date, class: 'datepicker1 %>
</td>
</tr>
<% end %>
对于 datepicker 使用 gem datepicker。日期选择器配置:
$('.datepicker1').datepicker({
regional: "en",
todayHighlight: true,
changeMonth: true,
todayHighlight: true,
language: "en",
orientation: "auto",
toggleActive: true,
autoclose: true
});
第一次加载页面日期字段: 在此处输入图像描述 此图像是第一次页面加载时的默认视图。
- 当我单击该字段然后打开 datpicker 并选择时,但日期被选择为
05/16/1923
. 正如我在 datepicker js 中设置的那样,不像配置的方式那样工作。
解决方案
文本值未显示,因为帮助程序在您的集合中找不到它。如果您的属性是一个字符串(正如@nitin-srivastava 所假设的那样),您必须传递一个字符串集合。
而且,根据这篇文章Rails 4 Best in place select collection 你必须通过这种形式的哈希集合:
{"txt1" => "txt1", "txt2" => "txt2", "txt3" => "txt3"}
这对我有用。
编辑:所以你可以这样写:
<%= best_in_place record, :gender, as: :select, collection: {'Male' => 'Male', 'Female' => 'Female', 'Other' => 'Other'} %>
如果 record.gender 等于 'Male'、'Female' 或 'Other',它将被显示。
推荐阅读
- loops - 带有 loop_control 和 loop_var Ansible 的循环变量
- amazon-web-services - 将 Google Analytics 导入 Redshift
- javascript - javascript 不向 ajax 发送变量只是发送 void 值
- java - 在 Eclipse 中更改 java.util.logging.Logger 输出的颜色和格式
- python - Numpy矩阵乘以不同的列
- python - While 循环条件中的变量应该退出循环,但不会
- javascript - 如何创建具有多级摘要和页脚值的 devextreme js 表
- c++ - C2065“未声明的标识符”来自创建、包含在项目中并被 IntelliSense 正确识别的类
- http - 未知权威签署的 x509 证书 - go-pingdom
- javascript - 在 javascript 中搜索具有 id 的孩子