我在迁移文件中为我的月份字段定义了日期数据类型
t.date :month
并在模型中检查其估值
validates :month, presence: true
在我的表单中,
= f.month_field :month, class: "form-control"
但是当我想在不保存的时候保存数据。当我调试我的 Controller 时
params[:rent]
=> {"item"=>"Room", "charge"=>"100", "payment_date(1i)"=>"2014", "payment_date(2i)"=>"12", "payment_date(3i)"=>"11", "month"=>"2014-11"}
检查月份字段。我们在那里很有值(value),但是当我尝试创造新记录时
@rent = current_user.rents.new(rent_params)
=> #<Rent id: nil, item: "Room", charge: 100, payment_date: "2014-12-11 00:00:00", month: nil, user_id: 1, created_at: nil, updated_at: nil>
月份字段为零。这是为什么??
我的 Controller 代码是
def paid
@rent = current_user.rents.new(rent_params)
if @rent.save
redirect_to payment_url(@rent)
else
render "pay"
end
end
private
def rent_params
params.require(:rent).permit(:item, :charge, :payment_date, :month)
end
我哪里错了吗??
最佳答案
amenzhinsky 是对的,但要扩展解决方案:
如果您只关心月数,请在迁移中更改:
t.date, :month
到
t.integer, :month
然后使用select_month在你的表格中。
或者你可以手动完成
select_tag('name', options_for_select([['January', 1], ['February', 2],...]))
关于ruby-on-rails - Rails month_field 数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27426033/