使用月份和年份制作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
Run Code Online (Sandbox Code Playgroud)
方法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"}
Run Code Online (Sandbox Code Playgroud)
方法2不保存给用户:
{"utf8"=>"?", "authenticity_token"=>"+0inJWFRPIr=", "user"=>{"phone_number"=>"7185289532"}, **"date"=>{"birth_year"=>"1945", "birth_month"=>"7"**}, "`commit"=>"Update User", "id"=>"10"}
Run Code Online (Sandbox Code Playgroud)
您可以在select_year或select_month中指定名称
= select_year(Date.today, {}, name: 'user[birth_year]')
= select_month(Date.today, {}, name: 'user[birth_month]')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
931 次 |
| 最近记录: |