Note that there are some explanatory texts on larger screens.

plurals
  1. PORails trying to convert a string to a datetime, but it's not saving to db
    primarykey
    data
    text
    <p>I'm trying to use a textfield as a place for users to edit their birthday as a date. With the example, i'm working on, no birthday exists yet. The birthday that i'm trying to add is <code>03/21/1986</code>. </p> <p>Here's the controller method:</p> <pre><code># PUT /contacts/1/edit # actually updates the users data def update_user @userProfile = User.find(params[:id]) @userDetails = @userProfile.user_details respond_to do |format| if @userProfile.update_attributes(params[:user]) format.html { flash[:success] = "Information updated successfully" redirect_to(edit_profile_path) } else format.html { flash[:error] = resource.errors.full_messages render :edit } end end end </code></pre> <p>and here's the model method. You can see i'm calling a validation method on :birthday to convert it to a date. Everything seems to work, but nothing saves to the database and i get no errors.</p> <pre><code># validate the birthday format def birthday_is_date p 'BIRTHDAY = ' p birthday_before_type_cast new_birthday = DateTime.strptime(birthday_before_type_cast, "%m/%d/%Y").to_date p new_birthday unless(Chronic.parse(new_birthday).nil?) errors.add(:birthday, "is invalid") end birthday = new_birthday end </code></pre> <p>Here's the printout from my p statements in my model validation method</p> <pre><code>"BIRTHDAY = " "03/21/1986" 1986-03-21 12:00:00 -0600 </code></pre> <p>I've also just noticed that if i do a date of <code>10/10/1980</code>, it works just fine, and if i do a date of <code>21/03/1986</code>, i get an <code>invalid date</code> error.</p> <p><strong>EDIT</strong> Here's some more info that may help:</p> <p>view:</p> <pre><code>&lt;%= form_for(@userProfile, :url =&gt; {:controller =&gt; "contacts", :action =&gt; "update_user"}, :html =&gt; {:class =&gt; "form grid_6 edit_profile_form"}, :method =&gt; :put ) do |f| %&gt; ... &lt;%= f.fields_for :user_details do |d| %&gt; &lt;%= d.label :birthday, raw("Birthday &lt;small&gt;mm/dd/yyyy&lt;/small&gt;") %&gt; &lt;%= d.text_field :birthday %&gt; ... &lt;% end %&gt; </code></pre> <p>user model</p> <pre><code>class User &lt; ActiveRecord::Base ... # Setup accessible (or protected) attributes for your model attr_accessible :email, :password, :password_confirmation, :remember_me, :username, :login, :home_phone, :cell_phone, :work_phone, :birthday, :home_address, :work_address, :position, :company, :user_details_attributes validates_presence_of :email has_one :user_details, :dependent =&gt; :destroy accepts_nested_attributes_for :user_details end </code></pre> <p>user_details model</p> <pre><code>require 'chronic' class UserDetails &lt; ActiveRecord::Base belongs_to :user validate :birthday_is_date attr_accessible :first_name, :last_name, :home_phone, :cell_phone, :work_phone, :birthday, :home_address, :work_address, :position, :company # validate the birthday format def birthday_is_date p 'BIRTHDAY = ' p birthday_before_type_cast new_birthday = DateTime.strptime(birthday_before_type_cast, "%m/%d/%Y").to_date p new_birthday unless(Chronic.parse(new_birthday).nil?) errors.add(:birthday, "is invalid") end birthday = new_birthday end end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload