验证错误后如何保留select_day的值?我可以像这样为它设置默认值:
f.select_day 1, {prefix: 'user[birthdate]'}
但我不能这样做来设置模型的值:
f.select_day :birthdate, {prefix: 'user[birthdate]'}
错误:
ActionView::Template::Error (undefined method `day' for :birthdate:Symbol)
是否有可能或者我需要手动从参数获取和设置值?
模型:
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
控制器:
def new
@user = User.new
end
def create
@user = User.build(params[:user])
end
看法:
= form_for @user do |f|
f.select_day 1, {prefix: 'user[birthdate]'}
f.select_month 1, {prefix: 'user[birthdate]'}
f.select_year 1990, {prefix: 'user[birthdate]'}