0
votes

I'm using this gem for my forms :

https://github.com/plataformatec/simple_form

I have 3 input fields each for day, month, year :

= f.input :dob_day, label_html: {style: 'display:none'}, placeholder: 'DD'
= f.input :dob_month, label_html: {style: 'display:none'}, placeholder: 'MM'
= f.input :dob_year, label_html: {style: 'display:none'}, placeholder: 'YYYY'

In my model (important bit) I'm trying to build a validations for the date of birth :

class User < ActiveRecord::Base

attr_writer :dob_day, :dob_month, :dob_year

  def dob_day
    @dob_day
  end

  def dob_month
    @dob_month
  end

  def dob_year
    @dob_year
  end

user.validates :dob_day, presence: true
user.validates :dob_month, presence: true
user.validates :dob_year, presence: true
user.validates :dob, presence: true,
date: { before: Proc.new { Time.now - 18.years }, message: ": You must be over 18 to join" }

Right now I get the error :

Can't mass-assign protected attributes: dob_day, dob_month, dob_year

I'd like to inject dob field with values from dob_day, dob_month, dob_year. I'd do it in model if possible rather than in controller

Am I doing this the wrong way? This should be fairly simple, but turns out its not

1
remove this as: :date, start_year: Date.today.year - 90, end_year: Date.today.year - 18, order: [:month, :year, :day]Nitin Jain
To prevent the mass-assign-error, you need to add: attr_accessible :dob_day, :dob_month, :dob_yearvidang

1 Answers

0
votes

you can remove

as: :date, start_year: Date.today.year - 90, end_year: Date.today.year - 18, order: [:month, :year, :day]

and then you can use jquery date picker for showing calendar to choose a dob