ruby-on-railsmongodbmongoid4

Date is not working in rails application with mongo id


I have create an rails application with mongoid but im facing one problem at DATE

I created an scaffold with a name "posting"

When im editing date it will updated....

I follow the instruction of Railscast #238 Mongoid here

there is my posting.rb file

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

this my _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 %>

and finally my show.html.erb file

<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 %>

Solution

  • What do you mean by not working? doesn't look like you have used published property in any of your views.

    in your show.html.erb you are using

    <%= f.date_select :pub %>
    

    and in your show.html.erb you are using

     <%= @posting.pub %>
    

    However there is no property called pub in your Posting model. What you have there is called published

    field :published, :type => Date

    You either need to rename it in the model, or in the views to match.