1

用月份和年份制作 form_for 的正确方法是什么。

这是保存到我的用户模型:birth_year 和 :birth_month。

我试过这两种方法:

= form_for @user do |f|
 = f.label :phone_number
 = f.text_field :phone_number
 %br
 / Method 1
 = f.label :date_of_birth
 = f.date_select :birth_month, :order => [:month] 
 = f.date_select :birth_year, :order => [:year]

 / Method 2
 = select_year(Date.today, field_name: 'birth_year')
 = select_month(Date.today, field_name: 'birth_year')
 %br
 %p.button
   = f.submit

方法一错误:

{"utf8"=>"✓", "_method"=>"put", "authenticity_token"=>"+0inJWFRPIrDt=", "user"=>{"phone_number"=>"7185289532", **"birth_month(1i)"=>"2013", "birth_month(3i)"=>"1", "birth_month(2i)"=>"6", "birth_year(2i)"=>"8", "birth_year(3i)"=>"27", "birth_year(1i)"=>"2000"}**, "commit"=>"Update User", "action"=>"update", "controller"=>"users", "id"=>"10"}

方法 2 不保存给用户:

{"utf8"=>"✓", "authenticity_token"=>"+0inJWFRPIr=", "user"=>{"phone_number"=>"7185289532"}, **"date"=>{"birth_year"=>"1945", "birth_month"=>"7"**}, "`commit"=>"Update User", "id"=>"10"}
4

1 回答 1

5

您可以在 select_year 或 select_month 中指定名称

= select_year(Date.today, {}, name: 'user[birth_year]')
= select_month(Date.today, {}, name: 'user[birth_month]')
于 2013-08-27T02:59:47.743 回答