Administrate: Invalid date error

Created on 21 Nov 2017  Â·  18Comments  Â·  Source: thoughtbot/administrate

I have a Post model with these attributes:

create_table :posts do |t|
      t.date :date
      t.text :rationale

      t.timestamps
end

date field is of type 'date' (Postgresql database)
After entering /admin/posts I get an error:

Showing /home/jedrek/.rvm/gems/ruby-2.3.0/gems/administrate-0.8.1/app/views/fields/date_time/_index.html.erb where line #20 raised:

invalid date

  #   "12/13/2012".to_date # => ArgumentError: invalid date
  def to_date
    ::Date.parse(self, false) unless blank?
  end

  # Converts a string to a DateTime value.

gem 'administrate', '~> 0.8.1'
Rails 5.0.5

I know for sure that it worked fine in gem "administrate", "~> 0.2.2"

Most helpful comment

Click 'Watch' on top to stay up-to-date with the thread. It's about the
issue you have.

12.02.2018 5:21 AM "Izak T" notifications@github.com napisał(a):

I'm confused about what you suggested me to watch. could you please be
more specific?

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/thoughtbot/administrate/issues/1029#issuecomment-364825605,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AFo1ibZMJbIbv-yFyrwgMtV4SFL4spULks5tT7w5gaJpZM4Ql8zl
.

All 18 comments

I had a typoo in my base code.

Would love to know what that typo was because I have the same exact problem.

Make sure you have the correct Date fields and timestamps set to Field::DateTime in your Dashboards.

ATTRIBUTE_TYPES = {
...
date: Field::DateTime.with_options(searchable: false),
created_at: Field::DateTime.with_options(searchable: false),
updated_at: Field::DateTime.with_options(searchable: false)
...
}.freeze

@filmratingapp

For admin_user_dashboard.rb:

require "administrate/base_dashboard"

class AdminUserDashboard < Administrate::BaseDashboard
  # ATTRIBUTE_TYPES
  # a hash that describes the type of each of the model's fields.
  #
  # Each different type represents an Administrate::Field object,
  # which determines how the attribute is displayed
  # on pages throughout the dashboard.
  ATTRIBUTE_TYPES = {
    posts: Field::HasMany.with_options(searchable: false),
    id: Field::Number.with_options(searchable: false),
    email: Field::String.with_options(searchable: true),
    password: Field::String.with_options(searchable: false),
    sign_in_count: Field::Number.with_options(searchable: false),
    current_sign_in_at: Field::DateTime.with_options(searchable: false),
    last_sign_in_at: Field::DateTime.with_options(searchable: false),
    current_sign_in_ip: Field::String.with_options(searchable: false),
    last_sign_in_ip: Field::String.with_options(searchable: false),
    first_name: Field::String.with_options(searchable: false),
    last_name: Field::String.with_options(searchable: false),
    created_at: Field::DateTime.with_options(searchable: false),
    updated_at: Field::DateTime.with_options(searchable: false),
    type: Field::String.with_options(searchable: false),
  }.freeze

user_dashboard.rb:

require "administrate/base_dashboard"

class UserDashboard < Administrate::BaseDashboard
  # ATTRIBUTE_TYPES
  # a hash that describes the type of each of the model's fields.
  #
  # Each different type represents an Administrate::Field object,
  # which determines how the attribute is displayed
  # on pages throughout the dashboard.
  ATTRIBUTE_TYPES = {
    posts: Field::HasMany.with_options(searchable: false),
    id: Field::Number.with_options(searchable: false),
    email: Field::String.with_options(searchable: true),
    password: Field::String.with_options(searchable: false),
    sign_in_count: Field::Number.with_options(searchable: false),
    current_sign_in_at: Field::DateTime.with_options(searchable: false),
    last_sign_in_at: Field::DateTime.with_options(searchable: false),
    current_sign_in_ip: Field::String.with_options(searchable: false),
    last_sign_in_ip: Field::String.with_options(searchable: false),
    first_name: Field::String.with_options(searchable: false),
    last_name: Field::String.with_options(searchable: false),
    created_at: Field::DateTime.with_options(searchable: false),
    updated_at: Field::DateTime.with_options(searchable: false),
    type: Field::String.with_options(searchable: false),
  }.freeze

post_dashboard.rb:

require "administrate/base_dashboard"

class PostDashboard < Administrate::BaseDashboard
  # ATTRIBUTE_TYPES
  # a hash that describes the type of each of the model's fields.
  #
  # Each different type represents an Administrate::Field object,
  # which determines how the attribute is displayed
  # on pages throughout the dashboard.
  ATTRIBUTE_TYPES = {
    user: Field::BelongsTo.with_options(searchable: false),
    id: Field::Number.with_options(searchable: false),
    date: Field::DateTime.with_options(searchable: false),
    rationale: Field::Text.with_options(searchable: true),
    created_at: Field::DateTime.with_options(searchable: false),
    updated_at: Field::DateTime.with_options(searchable: false),
    status: Field::DateTime.with_options(searchable: true),
  }.freeze

With the exception of status in post_dashboard.rb they are configured to your recommendations. I tried changing status to false and still had the same error.

When does this error message occur?

in your post_dashboard.rb

status: Field::DateTime.with_options(searchable: true)

is this correct?

2017-12-22 20:01 GMT+01:00 Daniel Cortes notifications@github.com:

@filmratingapp https://github.com/filmratingapp

For admin_user_dashboard.rb:

require "administrate/base_dashboard"

class AdminUserDashboard < Administrate::BaseDashboard
# ATTRIBUTE_TYPES
# a hash that describes the type of each of the model's fields.
#
# Each different type represents an Administrate::Field object,
# which determines how the attribute is displayed
# on pages throughout the dashboard.
ATTRIBUTE_TYPES = {
posts: Field::HasMany.with_options(searchable: false),
id: Field::Number.with_options(searchable: false),
email: Field::String.with_options(searchable: true),
password: Field::String.with_options(searchable: false),
sign_in_count: Field::Number.with_options(searchable: false),
current_sign_in_at: Field::DateTime.with_options(searchable: false),
last_sign_in_at: Field::DateTime.with_options(searchable: false),
current_sign_in_ip: Field::String.with_options(searchable: false),
last_sign_in_ip: Field::String.with_options(searchable: false),
first_name: Field::String.with_options(searchable: false),
last_name: Field::String.with_options(searchable: false),
created_at: Field::DateTime.with_options(searchable: false),
updated_at: Field::DateTime.with_options(searchable: false),
type: Field::String.with_options(searchable: false),
}.freeze

user_dashboard.rb:

require "administrate/base_dashboard"

class UserDashboard < Administrate::BaseDashboard
# ATTRIBUTE_TYPES
# a hash that describes the type of each of the model's fields.
#
# Each different type represents an Administrate::Field object,
# which determines how the attribute is displayed
# on pages throughout the dashboard.
ATTRIBUTE_TYPES = {
posts: Field::HasMany.with_options(searchable: false),
id: Field::Number.with_options(searchable: false),
email: Field::String.with_options(searchable: true),
password: Field::String.with_options(searchable: false),
sign_in_count: Field::Number.with_options(searchable: false),
current_sign_in_at: Field::DateTime.with_options(searchable: false),
last_sign_in_at: Field::DateTime.with_options(searchable: false),
current_sign_in_ip: Field::String.with_options(searchable: false),
last_sign_in_ip: Field::String.with_options(searchable: false),
first_name: Field::String.with_options(searchable: false),
last_name: Field::String.with_options(searchable: false),
created_at: Field::DateTime.with_options(searchable: false),
updated_at: Field::DateTime.with_options(searchable: false),
type: Field::String.with_options(searchable: false),
}.freeze

post_dashboard.rb:

require "administrate/base_dashboard"

class PostDashboard < Administrate::BaseDashboard
# ATTRIBUTE_TYPES
# a hash that describes the type of each of the model's fields.
#
# Each different type represents an Administrate::Field object,
# which determines how the attribute is displayed
# on pages throughout the dashboard.
ATTRIBUTE_TYPES = {
user: Field::BelongsTo.with_options(searchable: false),
id: Field::Number.with_options(searchable: false),
date: Field::DateTime.with_options(searchable: false),
rationale: Field::Text.with_options(searchable: true),
created_at: Field::DateTime.with_options(searchable: false),
updated_at: Field::DateTime.with_options(searchable: false),
status: Field::DateTime.with_options(searchable: true),
}.freeze

—
You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub
https://github.com/thoughtbot/administrate/issues/1029#issuecomment-353657187,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AFo1iWJaXTvoQ-LuC6fEJK9c9apy-1fhks5tC_yKgaJpZM4Ql8zl
.

@jedrekdomanski ,

The error occurs when I sign in as AdminUser which was created with my seeds.rb file:

@user = User.create(email: "[email protected]", password: "password", password_confirmation: "password", first_name: "Edward", last_name: "Snowden")

puts "1 User created"

AdminUser.create(email: "[email protected]", password: "password", password_confirmation: "password", first_name: "Admin", last_name: "User")

puts "1 Admin User created"

100.times do |post|
  Post.create!(date: Date.today, rationale: "#{post} rationale content", user_id: @user.id)
end

puts "100 Posts have been created"

So when I am signed in as an AdminUser, and I go to http://localhost:3000/admin/posts is when I get the error. I do not get the error if I go to posts as a @user.

Try
status: Field::Text.with_options(searchable: true)

2017-12-22 20:06 GMT+01:00 Daniel Cortes notifications@github.com:

@jedrekdomanski https://github.com/jedrekdomanski ,

The error occurs when I sign in as AdminUser which was created with my
seeds.rb file:

@user = User.create(email: "[email protected]", password: "password", password_confirmation: "password", first_name: "Edward", last_name: "Snowden")

puts "1 User created"

AdminUser.create(email: "[email protected]", password: "password", password_confirmation: "password", first_name: "Admin", last_name: "User")

puts "1 Admin User created"

100.times do |post|
Post.create!(date: Date.today, rationale: "#{post} rationale content", user_id: @user.id)
end

puts "100 Posts have been created"

So when I am signed in as an AdminUser, and I go to
http://localhost:3000/admin/posts

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/thoughtbot/administrate/issues/1029#issuecomment-353657924,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AFo1iRmdK5XvX3H_WUYuCaOZgd25iMkhks5tC_2_gaJpZM4Ql8zl
.

@jedrekdomanski

I would like to buy you a beer. Thank you.

To be clear to anyone who may have this problem in the future, upon the recommendation of @jedrekdomanski , I went into post_dashboard.rb and changed status: Field::DateTime.with_options(searchable: true), to status: Field::Text.with_options(searchable: true),

I followed what it was suggest here, yet I still have issue and get the following error.
Could anyone help me ?
my code snippet:

  ATTRIBUTE_TYPES = {
    user: Field::BelongsTo.with_options(searchable: false),
    id: Field::Number.with_options(searchable: false),
    date: Field::Text.with_options(searchable: true),<-------
    rationale: Field::Text.with_options(searchable: false),
    created_at: Field::DateTime.with_options(searchable: false),
    updated_at: Field::DateTime,
  }.freeze

lookupdate

Judging by the error message you have a data type error. Without looking at
you code I am unfortunately not able to help you.

lower("posts"."date") raises the error. Look at the data types in your
admin dashboard controller.

06.02.2018 5:38 PM "Izak T" notifications@github.com napisał(a):

I followed what it was suggest here, yet I still have issue and get the
following error.
Could anyone help me ?
[image: lookupdate]
https://user-images.githubusercontent.com/18540566/35871364-e46ec980-0b18-11e8-8746-7d7598f18593.png

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/thoughtbot/administrate/issues/1029#issuecomment-363482586,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AFo1if-0xKMYUgwy4q28vApsO1BeU7p7ks5tSH_ugaJpZM4Ql8zl
.

Sorry, gmail didn't send me your code snippet.

Try

date: Field::DateTime

Thank you for your fast response.
I tried date: Field::DateTime , yet the same issue has inspired again
my admin dashboard controller , but my question is why the error has happened when I look for date post?
If it is necessary, I will post the post dashboard as well.

require "administrate/base_dashboard"

class AdminUserDashboard < Administrate::BaseDashboard
  ATTRIBUTE_TYPES = {
    posts: Field::HasMany.with_options(searchable: false),
    id: Field::Number.with_options(searchable: false),
    email: Field::String.with_options(searchable: true),
    password: Field::String.with_options(searchable: false),
    sign_in_count: Field::Number.with_options(searchable: false),
    current_sign_in_at: Field::DateTime.with_options(searchable: false),
    last_sign_in_at: Field::DateTime.with_options(searchable: false),
    current_sign_in_ip: Field::String.with_options(searchable: false),
    last_sign_in_ip: Field::String.with_options(searchable: false),
    first_name: Field::String.with_options(searchable: false),
    last_name: Field::String.with_options(searchable: false),
    created_at: Field::DateTime.with_options(searchable: false),
    updated_at: Field::DateTime.with_options(searchable: false),
    type: Field::String.with_options(searchable: false),
  }.freeze

  COLLECTION_ATTRIBUTES = [
    :posts,
    :id,
    :email,
  ].freeze

  SHOW_PAGE_ATTRIBUTES = [
    :posts,
    :id,
    :email,
    :sign_in_count,
    :current_sign_in_at,
    :last_sign_in_at,
    :current_sign_in_ip,
    :last_sign_in_ip,
    :first_name,
    :last_name, 
    :created_at,
    :updated_at,
    :type,
  ].freeze

  FORM_ATTRIBUTES = [
    :email,
    :password,
    :first_name,
    :last_name,
    :type,
  ].freeze
end

Well, first of all it looks like the error is raised in your Admin::Posts class, not Admin::Users. Second, I've just spotted exact same issue on every admin page. No matter what I search by, it throws similar error. I would raise an issue with thoughtbot/administrate to let them know that the search feature does not work as it should.

should I post the Admin::Posts for ya?
Could you send me the link regarding how to report my issue?

When does your problem occur? When you try to search for something by particular data type? I.e. you search by date '2017-12-30'? Or by employee name? If yes, then you should raise and issue with the gem. It does not work for me either.

Check this thread, you might want to watch it.

2018-02-07 0:01 GMT+01:00 Izak T notifications@github.com:

should I post the Admin::Posts for ya?
Could you send me the link ?

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/thoughtbot/administrate/issues/1029#issuecomment-363595092,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AFo1iSi0oBtrHO0vy6SM94y9tvqGJPm7ks5tSNnGgaJpZM4Ql8zl
.

I'm confused about what you suggested me to watch. could you please be more specific?

Click 'Watch' on top to stay up-to-date with the thread. It's about the
issue you have.

12.02.2018 5:21 AM "Izak T" notifications@github.com napisał(a):

I'm confused about what you suggested me to watch. could you please be
more specific?

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/thoughtbot/administrate/issues/1029#issuecomment-364825605,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AFo1ibZMJbIbv-yFyrwgMtV4SFL4spULks5tT7w5gaJpZM4Ql8zl
.

Was this page helpful?
0 / 5 - 0 ratings