Rails试图将字符串转换为日期时间,但它没有保存到db

时间:2012-12-27 05:33:41

标签: ruby-on-rails ruby ruby-on-rails-3

我正在尝试使用文本字段作为用户将日期作为日期进行编辑的地方。通过这个例子,我正在努力,现在还没有生日。我正在尝试添加的生日是03/21/1986

这是控制器方法:

# 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

这是模型方法。您可以看到我正在调用验证方法:birthday将其转换为日期。一切似乎都有效,但没有任何东西保存到数据库中,我没有错误。

# 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

这是我的模型验证方法

中p语句的打印输出
"BIRTHDAY = "
"03/21/1986"
1986-03-21 12:00:00 -0600

我还注意到,如果我的约会时间为10/10/1980,则效果很好,如果我的日期为21/03/1986,则会出现invalid date错误。

修改 以下是一些可能有用的信息:

视图:

<%= form_for(@userProfile, :url => {:controller => "contacts", :action => "update_user"}, :html => {:class => "form grid_6 edit_profile_form"}, :method => :put ) do |f| %>
...
<%= f.fields_for :user_details do |d| %>
<%= d.label :birthday, raw("Birthday <small>mm/dd/yyyy</small>") %>                         
<%= d.text_field :birthday %>
...
<% end %>

用户模型

class User < 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 => :destroy
  accepts_nested_attributes_for :user_details
end

user_details模型

require 'chronic'

class UserDetails < 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

2 个答案:

答案 0 :(得分:0)

def birthday_is_date
    begin
        birthday = DateTime.strptime(birthday_before_type_cast, "%m/%d/%Y").to_date
    rescue
        errors.add(:birthday, "is invalid")
    end
end

答案 1 :(得分:0)

我最终使用虚拟属性,现在似乎正在运作。

我将此添加到我的模型中

attr_accessible :birthday_string

def birthday_string
    @birthday_string || birthday.strftime("%d-%m-%Y")
  end

  def birthday_string=(value)
    @birthday_string = value
    self.birthday = parse_birthday
  end

  private

  def birthday_is_date
    errors.add(:birthday_string, "is invalid") unless parse_birthday
  end

  def parse_birthday
    Chronic.parse(birthday_string)
  end