Last Updated: February 25, 2016
· mrako

Create Selection for a Year Range in Rails

I really love the simplicity of Rails when it comes to creating readable code and expressing your actual business requirements.

To give an example let's say you want to create an investment plan for an upcoming year. You can create the form and the selection with only two short lines (I'm using haml in the example):

= form_for @investment do |f|
  = f.select(:year, 2013..2023)

Naturally you can take it further by defining the variables in your controller. In edit-view the selected year is automatically populated because you're using form_for.

Say Thanks
Awesome Job

20838750 ca12 11e7 8395 3d07316c018d
Overijssel,Amsterdam, Arnhem
Overijssel, Amsterdam, Arnhem
Full Time