简体   繁体   中英

Ruby on Rails - Build time from params

I've this following params:

 {"utf8"=>"✓",
 "authenticity_token"=>"sPoWmnLU984Py9BEIe8MgEVo8tqPsSdLznFZ0dITXAw=",
 "event"=>{"title"=>"",
 "description"=>"",
 "day"=>"2013-11-21",
 "start(1i)"=>"2013",
 "start(2i)"=>"11",
 "start(3i)"=>"25",
 "start(4i)"=>"07",
 "start(5i)"=>"00",
 "end(1i)"=>"2013",
 "end(2i)"=>"11",
 "end(3i)"=>"25",
 "end(4i)"=>"07",
 "end(5i)"=>"00",
 "day_id"=>"day"},
 "commit"=>"Event erstellen"}

and my controller:

start  = "#{params[:event][:start(4i)]}:#{params[:event][:start(5i)]}"

I want only the times attributes from the parameters, so that at the end i have something like this:

07:00

If "start" is a DateTime attribute of the model, you could simply use the strftime method of the dateTime. Something like start.strftime("%H:%M")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM