【问题标题】:In place editing of Time object时间对象的就地编辑
【发布时间】:2012-12-09 18:52:27
【问题描述】:

我有一个应用程序,用于为许多员工计划轮班。我有一个Shift 模型,我将shiftstartshiftend 存储为Time 对象。

我将用户Shifts 显示在一个表格中显示几天的页面上。这个想法是用户可以使用Best In Place gem 直接在此表中编辑shiftstartshiftend

但在处理 Time 对象时,我似乎无法弄清楚如何让它工作 - 任何人都对此有一些经验或建议。

我有以下代码:

查看:

<% @myShifts.where(:shiftdate => date).order(:shiftstart).each do |shift| %>
  <div class="shift-item span">
    <div class="hider">
      <p>
        <i class="icon gear"></i>
          <%= best_in_place shift.shiftstart.strftime("%H.%M"), :type => :input %> - <%= best_in_place shift.shiftend.strftime("%H.%M"), :type => :input %>
      </p>
    </div>
  </div>
<% end %>

架构:

create_table "shifts", :force => true do |t|
  t.time     "shiftstart"
  t.time     "shiftend"
  t.integer  "type_id"
  t.integer  "user_id"
  t.string   "note"
  t.datetime "created_at", :null => false
  t.datetime "updated_at", :null => false
  t.date     "shiftdate"
end

我收到“undefined method `{:type=>:input}' for "08.00":String”错误,我已经尝试将 Best In Place Type 切换为其他类型输入 - 没有帮助..

【问题讨论】:

    标签: ruby-on-rails time edit-in-place best-in-place


    【解决方案1】:

    best_in_place 方法接受两个强制参数;一个对象(在你的情况下为shift)和一个字段(shiftstartshiftend),然后是可选的选项哈希(:type =&gt; :input)。所以你的代码需要是这样的:

    <%= best_in_place shift, :shiftstart, :type => :input, :display_with => Proc.new { |f| f.strftime("%H.%M") } %>
    

    【讨论】:

    • 这给了我一个错误:#:0x007fe653ce4460> 的未定义方法 `shift_path'.. 有什么想法吗?
    • 可能您没有使用ShiftsController 来处理 shift 的操作。您可以将您的实际班次路径(处理 PUT 请求的路径)提供给 best_in_place 选项,例如 :path =&gt; your_shift_path(shift)。或者您可以提供您的rake routes 以获得更多见解:)。
    • 发现问题..我的路线不对..但您的解决方案似乎有效..完美.. :)
    • 但是你知道如何只显示时间 - 例如“08:30”在编辑模式下..现在它显示日期“2000-01-01”、时间和“ +100" UTC 东西 :)
    • 这有点骇人听闻。我没有看到 BIP 有直接的方法来做到这一点,所以我认为您必须使用 JavaScript 将 BIP 元素上的 data-original-content 属性更改为您希望显示为可编辑的值。
    猜你喜欢
    • 2020-06-22
    • 1970-01-01
    • 2013-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多