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
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 %>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:
- 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
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 %>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:
- 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