Zephyr4434
Zephyr4434

Reputation: 796

Rails - how to set default on edit form for a year select form

In my app, I let people select their start year for a job. This works fine when creating a new job, but on the edit form I want the year the user selected during create to be automatically population.

My code:

<%= select_year Date.today, start_year: Time.now.year, end_year: Time.now.year - 95, field_name: :start, prefix: :job %>

Currently, it just defaults to 2013 everytime. I know I am passing in the instance variable correctly because the other fields are populated automatically, just not this year dropdown.

How do I get this to work?

Upvotes: 0

Views: 325

Answers (1)

Justin
Justin

Reputation: 1956

This answer was too long to put in the comments, so I'm putting it here as an answer, but let me know if it answers your question or not.

You could manually set the start_year and end_year variables to, for example, 1950 and Time.now.year. Then, set the first argument in select_year helper to the instance variable to make it the default selection between the wide range instead of Date.today.

For example:

<%= select_year @instance_variable, start_year: 1950, end_year: Time.now.year, field_name: :start, prefix: :job %>

More examples are available in the Rails Docs, too: http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#method-i-select_year

Upvotes: 1

Related Questions