Converting String to Date before save with Rails
Asked Answered
P

2

9

Very new to Ruby and I've been stuck for hours. Searching everywhere and can't find an answer.

So I'm using the bootstrap datepicker for rails gem.

Because I've changed the date format of the datepicker, it won't store in the DB. Guessing this is because the simple_form input is being used as a string to avoid the default date selection inputs applied by simple_form.

My question is: How do I modify/convert a string like "06/18/2013" to a date before it is saved to the db? Is this best handled by the controller?

My controller:

# PUT /events/1
# PUT /events/1.json
def update
  @event = Event.find(params[:id])

  # Ugghhhh I need help
  # @event.event_date = Date.parse(params[:event_date]).to_date

  respond_to do |format|
    if @event.update_attributes(params[:event])
      format.html { redirect_to @event, :notice => 'Event was successfully updated.' }
      format.json { head :no_content }
    else
      format.html { render :action => "edit" }
      format.json { render :json => @event.errors, :status => :unprocessable_entity }
    end
  end
end

DB Schema:

  create_table "events", :force => true do |t|
    t.string   "event_name"
    t.string   "event_location"
    t.date     "event_date"
    t.time     "event_time"
    t.text     "event_content"
    t.datetime "created_at",                       :null => false
    t.datetime "updated_at",                       :null => false
    t.integer  "user_id"
    t.boolean  "approved",       :default => true
    t.integer  "category_id"
  end

This is the server log:

  Started PUT "/events/1" for 127.0.0.1 at 2013-06-14 02:37:15 -0700
  Processing by EventsController#update as HTML
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"RYMdJ0lFmvG0+nVIsTtJXu5fyD/L3/WAKsk9FX6WWgo=", "event"=>{"user_id"=>"1", "category_id"=>"3", "event_name"=>"A Event Name", "event_location"=>"Event Location", "event_date"=>"06/13/2013", "event_time(1i)"=>"2000", "event_time(2i)"=>"1", "event_time(3i)"=>"1", "event_time(4i)"=>"02", "event_time(5i)"=>"18", "event_content"=>"First night"}, "commit"=>"Update Event", "id"=>"1"}
    User Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
    Event Load (0.1ms)  SELECT "events".* FROM "events" WHERE "events"."id" = ? LIMIT 1  [["id", "1"]]
     (0.0ms)  begin transaction
     (0.0ms)  commit transaction
  Redirected to http://localhost:3000/events/1
  Completed 302 Found in 4ms (ActiveRecord: 0.3ms)
Public answered 14/6, 2013 at 9:43 Comment(2)
I would just suggest you maybe change field names, it's redundant to call Event attribute event_name, event_location, event_...Fractionate
Yea, agreed. Thanks for the tip.Public
R
17

You can use strptime in the controller.

DateTime.strptime("06/18/2013", "%m/%d/%Y")
Rainwater answered 14/6, 2013 at 9:47 Comment(3)
That's what I did when had same problem.Fractionate
I'm getting the error: no implicit conversion of nil into StringPublic
So probably first argument is not passed. Check if value is set first, to add .to_s (but that may fail as well or return 1 Jan 1970)Fractionate
D
0

The correct one should be

DateTime.strptime( "02/06/2014", "%d/%m/%Y").strftime("%Y-%m-%d")

Double answered 17/7, 2014 at 6:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.