qzyxpa
Last Updated: February 25, 2016
·
2.747K
· 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.