首页 > 解决方案 > 如何使用 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
});

第一次加载页面日期字段: 在此处输入图像描述 此图像是第一次页面加载时的默认视图。

  1. 当我单击该字段然后打开 datpicker 并选择时,但日期被选择为05/16/1923. 正如我在 datepicker js 中设置的那样,不像配置的方式那样工作。

在此处输入图像描述

标签: ruby-on-railsrubyruby-on-rails-6

解决方案


文本值未显示,因为帮助程序在您的集合中找不到它。如果您的属性是一个字符串(正如@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',它将被显示。


推荐阅读