1

I am learning rails and have recently come across a road block. Any guidance is appreciated.

I am using device and I have a :confirmed_at DateTime column as a protected attribute. So to update it from my admin interface I super the controller update method and add:

      @user.update_attribute(:confirmed_at, params[:user][:confirmed_at])
      @user.update_attribute(:school_id, params[:user][:school_id])

:school_id gets update correctly but :confirmed_at, which is datetime, is always nil. I looked at the params hash and sure enough it does not have :confirmed_at but it has

      "confirmed_at(1i)"=>"2012", 
      "confirmed_at(2i)"=>"6", 
      "confirmed_at(3i)"=>"2", 
      "confirmed_at(4i)"=>"04",   
      "confirmed_at(5i)"=>"11"

Rails doesn't seem to be translating the parameters for the fields into datetime objects for the database.

** How do I capture these value to update my :confirmed_at field properly?

Any help is appreciated.

Rails 3.1, Ruby 1.9

Yuri
  • 1,261
  • 1
  • 11
  • 23

2 Answers2

1

I guess you're using datetime_select helper.

Check this out: Where is the Rails method that converts data from `datetime_select` into a DateTime object?

Community
  • 1
  • 1
Alexey Kharchenko
  • 1,032
  • 5
  • 10
  • Thank you Alexey. I am not sure handling the conversion manually is appropriate though. Correct me if I am wrong but this seem to suggest I should get the params individually and manually? – Yuri Jun 06 '12 at 18:24
1

Try this:

@user.update_attribute(:confirmed_at, params[:confirmed_at])

The date_select is sent in its own params and not as part of the params[:user] hash.

I hope this helps.

Anil
  • 3,899
  • 1
  • 20
  • 28
  • Did not quite do it. An ugly solution so far is to add the fiels as a textfield and just type the date into as 12/12/12. – Yuri Jun 06 '12 at 18:17
  • Thank you Anil. I thought this would work because it makes sense and I had not tried it before reading your comment. – Yuri Jun 06 '12 at 18:26
  • @Yuri So what is your final solution? – Anil Jun 06 '12 at 19:40
  • I added a field as :string and just add the value in human format (12/12/12...) and it converts and save it fine. I don't like it but since is for the app Admin interface where only I really have access it is fine. Maybe I should put this in an answer. – Yuri Jun 07 '12 at 21:59