Rails decoding dates out of the session?
I've used date_select
to create page with three fields to create a date, and so my session ends up with the following fields in it:
{"CompanyUk"=>{"company_number"=>"12345",
"incorporation_date(1i)"=>"2011",
"incorporation_date(2i)"=>"5",
"incorporation_date(3i)"=>"27"}}
What is a good way to reconstruct the date from the three separate fields? Obviously, I can 开发者_如何学JAVApull them out one-by-one and reassemble, but that seems like a lot of extra code over what would be possible.
((1..3).map { |i| session['CompanyUk']["incorporation_date(#{i}i)"] } * '/').to_date
I had:
datemap = params["CompanyUk"].select {|k,v| k.start_with? "incorporation_date"}
inc_date = Date.new(*(datemap.values.map {|x| x.to_i}))
Which is rather more complex than Mori's solution.
Alternatively, based on Mori's solution, but avoiding the string concatenation:
Date.new *((1..3).map { |i| params['CompanyUk']["incorporation_date(#{i}i)"].to_i })
精彩评论