Rails - 制作一年和一个月的下拉菜单

时间:2013-08-27 02:43:29

标签: ruby-on-rails

使用月份和年份制作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

方法1错误:

{"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"}

1 个答案:

答案 0 :(得分:5)

您可以在select_year或select_month

中指定名称
= select_year(Date.today, {}, name: 'user[birth_year]')
= select_month(Date.today, {}, name: 'user[birth_month]')