在 ID 为 mongo 的 rails 应用程序中,日期无效

Date is not working in rails application with mongo id

我已经用 mongoid 创建了一个 rails 应用程序,但我在 DATE

遇到了一个问题

我创建了一个名为“posting”的脚手架

当我编辑日期时它会更新....

我遵循 Railscast #238 Mongoid 的说明 here

有我的posting.rb文件

class Posting
  include Mongoid::Document
  field :title
  field :description
  field :comments
  field :published, :type => Date
end

这是我的_from.html.erb

<%= form_for(@posting) do |f| %>
  <% if @posting.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@posting.errors.count, "error") %> prohibited this posting from being saved:</h2>

      <ul>
      <% @posting.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :title %><br>
    <%= f.text_field :title %>
  </div>
  <div class="field">
    <%= f.label :description %><br>
    <%= f.text_field :description %>
  </div>
  <div class="field">
    <%= f.label :published %><br>
    <%= f.date_select :published %>
  </div>
  <div class="field">
    <%= f.label :comments %><br>
    <%= f.text_area :comments %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

最后是我的 show.html.erb 文件

<p id="notice"><%= notice %></p>

<p>
  <strong>Title:</strong>
  <%= @posting.title %>
</p>

<p>
  <strong>Description:</strong>
  <%= @posting.description %>
</p>
<p>
  <strong>published:</strong>
  <%= @posting.published %>
</p>

<p>
  <strong>Comments:</strong>
  <%= @posting.comments %>
</p>

<%= link_to 'Edit', edit_posting_path(@posting) %> |
<%= link_to 'Back', postings_path %>

不工作是什么意思?您似乎没有在任何视图中使用过 published 属性。

在您的 show.html.erb 中,您正在使用

<%= f.date_select :pub %>

并且在您的 show.html.erb 中您正在使用

 <%= @posting.pub %>

但是在您的 Posting 模型中没有 属性 称为 pub。你有什么叫做 published

field :published, :type => Date

您需要在模型或视图中重命名它以匹配。