# Using select\_year in form\_for helper block

**URL:** <https://www.ruby-forum.com/t/using-select-year-in-form-for-helper-block/139349>\
**Category:** Rails\
**Created:** [June 3, 2008, 4:40am UTC](https://www.ruby-forum.com/t/using-select-year-in-form-for-helper-block/139349 "2008-06-03T04:40:47Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![taro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/df705f/32.png) [@taro](https://www.ruby-forum.com/u/taro)\
**Post date:** [June 3, 2008, 4:40am UTC](https://www.ruby-forum.com/t/using-select-year-in-form-for-helper-block/139349/1 "2008-06-03T04:40:47Z")

</div>

## I’m having trouble trying to implement a selection field of years in a form\_for block for Views. Initially, I wrote something like this:

# Sign-up

 \<% array\_of\_years = generate\_year\_array() %\> \<% form\_for :person do |form| %\> 
## First Name
 \<%= form.text\_field :first\_name, :size =\> 30 %\> 
## Last Name
 \<%= form.text\_field :last\_name, :size =\> 30 %\> 
## RIN
 \<%= form.text\_field :last\_name, :size =\> 10 %\> 
## Email
 \<%= form.text\_field :last\_name, :size =\> 30, :value =\> '@rpi.edu' %\> 
## Graduation Year
 \<%= form.select :year, array\_of\_years %\>   
 \<%= submit\_tag "Join In!" %\> \<%end%\>
--------
## With the helper, generate\_year\_array() as:

# Methods added to this helper will be available to all templates in

the application.  
module ApplicationHelper

#generate an array years from 4 years past to 8 years in future  
def generate\_year\_array( year = nil )  
if year  
default\_year = year.to\_i  
else  
default\_year = Time.now.year + 4  
end  
begin\_year = default\_year - 8  
end\_year = default\_year + 4  
output = []  
begin\_year.upto( end\_year ) do |this\_year|  
output.push this\_year.to\_s  
end  
end

## end

## This, unfortunately, gave me the following error:

1. Error:  
test\_valid\_valid\_sign\_up(SignUpControllerTest):  
ActionView::TemplateError: undefined method `inject’ for 2004:Fixnum  
On line #26 of sign\_up/sign\_up.rhtml

```
23: <h2>
24: Graduation Year
25: </h2>
26: <%= form.select :year, array_of_years %>
27: <br />
28: <%= submit_tag "Join In!" %>
29: <%end%>

c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_view/

```

## helpers/form\_options\_helper.rb:161:in `options_for_select' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_view/ helpers/form_options_helper.rb:358:in`to\_select\_tag’ c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action\_view/ helpers/form\_options\_helper.rb:84:in `select' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_view/ helpers/form_options_helper.rb:409:in`select’ app/views/sign\_up/sign\_up.rhtml:26:in `_run_erb_47app47views47sign_up47sign_up46rhtml' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_view/ helpers/form_helper.rb:248:in`fields\_for’ c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action\_view/ helpers/form\_helper.rb:184:in `form_for' app/views/sign_up/sign_up.rhtml:6:in`\_run\_erb\_47app47views47sign\_up47sign\_up46rhtml’ c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action\_view/ base.rb:637:in `send' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_view/ base.rb:637:in`compile\_and\_render\_template’ c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action\_view/ base.rb:365:in `render_template' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_view/ base.rb:316:in`render\_file’ c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/ action\_controller/base.rb:1100:in `render_for_file' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/ action_controller/base.rb:836:in`render\_with\_no\_layout’ c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/ action\_controller/layout.rb:262:in `render_without_benchmark' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/ action_controller/benchmarking.rb:51:in`render’ c:/ruby/lib/ruby/1.8/benchmark.rb:293:in `measure' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/ action_controller/benchmarking.rb:51:in`render’ c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/ action\_controller/base.rb:1153:in `default_render' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/ action_controller/base.rb:1159:in`perform\_action\_without\_filters’ c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/ action\_controller/filters.rb:697:in `call_filters' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/ action_controller/filters.rb:689:in`perform\_action\_without\_benchmark’ c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/ action\_controller/benchmarking.rb:68:in `perform_action_without_rescue' c:/ruby/lib/ruby/1.8/benchmark.rb:293:in`measure’ c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/ action\_controller/benchmarking.rb:68:in `perform_action_without_rescue' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/ action_controller/rescue.rb:199:in`perform\_action\_without\_caching’ c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/ action\_controller/caching.rb:678:in `perform_action' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2-/lib/ active_record/connection_adapters/abstract/query_cache.rb:33:in`cache’ c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2-/lib/ active\_record/query\_cache.rb:8:in `cache' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/ action_controller/caching.rb:677:in`perform\_action’ c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/ action\_controller/base.rb:524:in `send' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/ action_controller/base.rb:524:in`process\_without\_filters’ c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/ action\_controller/filters.rb:685:in `process_without_session_management_support' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/ action_controller/session_management.rb:123:in`process\_without\_test’ c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/ action\_controller/test\_process.rb:15:in `process' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/ action_controller/test_process.rb:393:in`process’ c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/ action\_controller/test\_process.rb:364:in `post' test/functional/sign_up_controller_test.rb:9:in`test\_valid\_valid\_sign\_up’ c:/ruby/lib/ruby/1.8/test/unit/testcase.rb:78:in ` __send__' c:/ruby/lib/ruby/1.8/test/unit/testcase.rb:78:in`run’ c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.0.2-/lib/ active\_support/testing/default.rb:7:in `run' c:/ruby/lib/ruby/1.8/test/unit/testsuite.rb:34:in`run’ c:/ruby/lib/ruby/1.8/test/unit/testsuite.rb:33:in `each' c:/ruby/lib/ruby/1.8/test/unit/testsuite.rb:33:in`run’ c:/ruby/lib/ruby/1.8/test/unit/testsuite.rb:34:in `run' c:/ruby/lib/ruby/1.8/test/unit/testsuite.rb:33:in`each’ c:/ruby/lib/ruby/1.8/test/unit/testsuite.rb:33:in `run' c:/ruby/lib/ruby/1.8/test/unit/ui/testrunnermediator.rb:46:in`run\_suite’ c:/ruby/lib/ruby/1.8/test/unit/ui/console/testrunner.rb:67:in `start_mediator' c:/ruby/lib/ruby/1.8/test/unit/ui/console/testrunner.rb:41:in`start’ c:/ruby/lib/ruby/1.8/test/unit/ui/testrunnerutilities.rb:29:in `run' c:/ruby/lib/ruby/1.8/test/unit/autorunner.rb:216:in`run’ c:/ruby/lib/ruby/1.8/test/unit/autorunner.rb:12:in `run’ c:/ruby/lib/ruby/1.8/test/unit.rb:278 c:/ruby/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake/ rake\_test\_loader.rb:5

## Lo and behold, a little search helped me find a better solution: select\_year and fields\_for. However, I’m clearly doing something wrong in the example I’ll bring up next, yet the example is a little too vague to figure out what I mistake I’ve made. Can anyone help me out on this? The code I’ve attempted to use:

# Edit

 \<% form\_for :person do |form| %\> 
## First Name
 \<%= form.text\_field :first\_name, :size =\> 30 %\> 
## Last Name
 \<%= form.text\_field :last\_name, :size =\> 30 %\> 
## RIN
 \<%= form.text\_field :last\_name, :size =\> 10 %\> 
## Email
 \<%= form.text\_field :last\_name, :size =\> 30 %\> 
## Graduation Year
  
 \<% fields\_for @person.year do |year\_field| %\> \<%= year\_field.select\_year( Time.now.year + 4, :start\_year =\> Time.now.year - 4, :end\_year =\> Time.now.year + 8 ) %\> \<% end %\> \<%= submit\_tag "Join In!" %\> \<%end%\>
---------
## error message:

1. Error:  
test\_no\_param\_edit\_member(MemberListControllerTest):  
ActionView::TemplateError: undefined method `inject’ for 2004:Fixnum  
On line #26 of member\_list/edit\_member.rhtml

```
23: <h2>
24: Graduation Year
25: </h2>
26: <%= form.select :year, array_of_years %>
27: <br />
28: <%= submit_tag "Join In!" %>
29: <%end%>

c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_view/

```

helpers/form\_options\_helper.rb:161:in `options_for_select' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_view/ helpers/form_options_helper.rb:358:in`to\_select\_tag’  
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action\_view/  
helpers/form\_options\_helper.rb:84:in `select' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_view/ helpers/form_options_helper.rb:409:in`select’  
app/views/member\_list/edit\_member.rhtml:26:in  
`_run_erb_47app47views47member_list47edit_member46rhtml' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_view/ helpers/form_helper.rb:248:in`fields\_for’  
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action\_view/  
helpers/form\_helper.rb:184:in `form_for' app/views/member_list/edit_member.rhtml:6:in`\_run\_erb\_47app47views47member\_list47edit\_member46rhtml’  
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action\_view/  
base.rb:637:in `send' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_view/ base.rb:637:in`compile\_and\_render\_template’  
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action\_view/  
base.rb:365:in `render_template' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_view/ base.rb:316:in`render\_file’  
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/  
action\_controller/base.rb:1100:in `render_for_file' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/ action_controller/base.rb:858:in`render\_with\_no\_layout’  
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/  
action\_controller/base.rb:872:in `render_with_no_layout' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/ action_controller/layout.rb:262:in`render\_without\_benchmark’  
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/  
action\_controller/benchmarking.rb:51:in `render' c:/ruby/lib/ruby/1.8/benchmark.rb:293:in`measure’  
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/  
action\_controller/benchmarking.rb:51:in `render' app/controllers/member_list_controller.rb:20:in`edit\_member’  
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/  
action\_controller/base.rb:1158:in `send' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/ action_controller/base.rb:1158:in`perform\_action\_without\_filters’  
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/  
action\_controller/filters.rb:697:in `call_filters' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/ action_controller/filters.rb:689:in`perform\_action\_without\_benchmark’  
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/  
action\_controller/benchmarking.rb:68:in  
`perform_action_without_rescue' c:/ruby/lib/ruby/1.8/benchmark.rb:293:in`measure’  
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/  
action\_controller/benchmarking.rb:68:in  
`perform_action_without_rescue' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/ action_controller/rescue.rb:199:in`perform\_action\_without\_caching’  
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/  
action\_controller/caching.rb:678:in `perform_action' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2-/lib/ active_record/connection_adapters/abstract/query_cache.rb:33:in`cache’  
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2-/lib/  
active\_record/query\_cache.rb:8:in `cache' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/ action_controller/caching.rb:677:in`perform\_action’  
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/  
action\_controller/base.rb:524:in `send' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/ action_controller/base.rb:524:in`process\_without\_filters’  
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/  
action\_controller/filters.rb:685:in  
`process_without_session_management_support' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/ action_controller/session_management.rb:123:in`process\_without\_test’  
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/  
action\_controller/test\_process.rb:15:in `process' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/ action_controller/test_process.rb:393:in`process’  
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/  
action\_controller/test\_process.rb:364:in `post' test/functional/member_list_controller_test.rb:203:in`test\_no\_param\_edit\_member’  
c:/ruby/lib/ruby/1.8/test/unit/testcase.rb:78:in ` __send__' c:/ruby/lib/ruby/1.8/test/unit/testcase.rb:78:in`run’  
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.0.2-/lib/  
active\_support/testing/default.rb:7:in `run' c:/ruby/lib/ruby/1.8/test/unit/testsuite.rb:34:in`run’  
c:/ruby/lib/ruby/1.8/test/unit/testsuite.rb:33:in `each' c:/ruby/lib/ruby/1.8/test/unit/testsuite.rb:33:in`run’  
c:/ruby/lib/ruby/1.8/test/unit/testsuite.rb:34:in `run' c:/ruby/lib/ruby/1.8/test/unit/testsuite.rb:33:in`each’  
c:/ruby/lib/ruby/1.8/test/unit/testsuite.rb:33:in `run' c:/ruby/lib/ruby/1.8/test/unit/ui/testrunnermediator.rb:46:in`run\_suite’  
c:/ruby/lib/ruby/1.8/test/unit/ui/console/testrunner.rb:67:in  
`start_mediator' c:/ruby/lib/ruby/1.8/test/unit/ui/console/testrunner.rb:41:in`start’  
c:/ruby/lib/ruby/1.8/test/unit/ui/testrunnerutilities.rb:29:in  
`run' c:/ruby/lib/ruby/1.8/test/unit/autorunner.rb:216:in`run’  
c:/ruby/lib/ruby/1.8/test/unit/autorunner.rb:12:in `run’  
c:/ruby/lib/ruby/1.8/test/unit.rb:278  
c:/ruby/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake/  
rake\_test\_loader.rb:5
