ruby on rails - select_day/year/month preset value -
how can preserve value of select_day after validation error? can set default value this:
f.select_day 1, {prefix: 'user[birthdate]'}
but can't set value model:
f.select_day :birthdate, {prefix: 'user[birthdate]'}
error:
actionview::template::error (undefined method `day' :birthdate:symbol)
is possible or need , set value params manually?
model:
class user include activemodel::model include activerecord::attributeassignment attr_accessor :birthdate def self.build(request, params = {}) u = self.new u.assign_attributes(params) u end end
controller:
def new @user = user.new end def create @user = user.build(params[:user]) end
view:
= form_for @user |f| f.select_day 1, {prefix: 'user[birthdate]'} f.select_month 1, {prefix: 'user[birthdate]'} f.select_year 1990, {prefix: 'user[birthdate]'}
if check documentation select_day (and _month , _year) you'll see methods normally accept date first parameter. accept integer you've chosen decidedly alternate implementation.
be may, i'd suggest couple of small changes. first, i'd consider setting default birthdate user class along these lines.
class user attr_accessor :birthdate def initialize() @birthdate = date.new 1990, 1, 1 super end ... end
then forms can make use of user#birthdate accessor in both new , edit views.
= form_for @user |f| = f.select_day @user.birthdate, prefix: 'user[birthdate]' = f.select_month @user.birthdate, prefix: 'user[birthdate]' = f.select_year @user.birthdate, prefix: 'user[birthdate]'
Comments
Post a Comment