very new to Ruby/Rails and trying to figure out Chapter 3/4

4 views
Skip to first unread message

gumpster

unread,
Aug 11, 2008, 8:56:27 PM8/11/08
to Beginning Ruby on Rails E-Commerce
Hi all,

I'm using Rails 2.0,2 with Ruby 1.8.6 (Gems 0.8.4) and I'm having
trouble in Chapter 3/4 with integration tests:

ActionView::TemplateError (You have a nil object when you didn't
expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.map) on line #7 of admin/book/
_form.html.erb:
4: <%= text_field 'book', 'title' %></p>
5:
6: <p><label for="book_publisher">Publisher</label><br/>
7: <%= collection_select :book, :publisher_id, @publishers, :id, :name
%></p>
8:
9: <p><label for="book[author_ids][]">Authors</label><br/>
10: <%= select_tag 'book[author_ids][]',

/var/lib/gems/1.8/gems/actionpack-2.0.2/lib/action_view/helpers/
form_options_helper.rb:180:in `options_from_collection_for_select'
/var/lib/gems/1.8/gems/actionpack-2.0.2/lib/action_view/helpers/
form_options_helper.rb:366:in `to_collection_select_tag'
/var/lib/gems/1.8/gems/actionpack-2.0.2/lib/action_view/helpers/
form_options_helper.rb:118:in `collection_select'
app/views/admin/book/_form.html.erb:7:in
`_run_erb_47app47views47admin47book47_form46html46erb'
/var/lib/gems/1.8/gems/actionpack-2.0.2/lib/action_view/base.rb:
637:in `send'
/var/lib/gems/1.8/gems/actionpack-2.0.2/lib/action_view/base.rb:
637:in `compile_and_render_template'
/var/lib/gems/1.8/gems/actionpack-2.0.2/lib/action_view/base.rb:
365:in `render_template'
/var/lib/gems/1.8/gems/actionpack-2.0.2/lib/action_view/base.rb:
316:in `render_file'
/var/lib/gems/1.8/gems/actionpack-2.0.2/lib/action_view/base.rb:
331:in `render'
/var/lib/gems/1.8/gems/actionpack-2.0.2/lib/action_view/
partials.rb:117:in `render_partial'
/var/lib/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/
benchmarking.rb:26:in `benchmark'
/usr/lib/ruby/1.8/benchmark.rb:293:in `measure'
/usr/lib/ruby/1.8/benchmark.rb:307:in `realtime'
/var/lib/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/
benchmarking.rb:26:in `benchmark'
/var/lib/gems/1.8/gems/actionpack-2.0.2/lib/action_view/
partials.rb:116:in `render_partial'
/var/lib/gems/1.8/gems/actionpack-2.0.2/lib/action_view/base.rb:
352:in `render'
app/views/admin/book/edit.html.erb:4:in
`_run_erb_47app47views47admin47book47edit46html46erb'
/var/lib/gems/1.8/gems/actionpack-2.0.2/lib/action_view/helpers/
capture_helper.rb:142:in `call'
/var/lib/gems/1.8/gems/actionpack-2.0.2/lib/action_view/helpers/
capture_helper.rb:142:in `capture_erb_with_buffer'
/var/lib/gems/1.8/gems/actionpack-2.0.2/lib/action_view/helpers/
capture_helper.rb:44:in `capture'
/var/lib/gems/1.8/gems/actionpack-2.0.2/lib/action_view/helpers/
form_tag_helper.rb:417:in `form_tag_in_block'
/var/lib/gems/1.8/gems/actionpack-2.0.2/lib/action_view/helpers/
form_tag_helper.rb:39:in `form_tag'
app/views/admin/book/edit.html.erb:3:in
`_run_erb_47app47views47admin47book47edit46html46erb'
/var/lib/gems/1.8/gems/actionpack-2.0.2/lib/action_view/base.rb:
637:in `send'
/var/lib/gems/1.8/gems/actionpack-2.0.2/lib/action_view/base.rb:
637:in `compile_and_render_template'
/var/lib/gems/1.8/gems/actionpack-2.0.2/lib/action_view/base.rb:
365:in `render_template'
/var/lib/gems/1.8/gems/actionpack-2.0.2/lib/action_view/base.rb:
316:in `render_file'
/var/lib/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/
base.rb:1100:in `render_for_file'
/var/lib/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/
base.rb:858:in `render_with_no_layout'
/var/lib/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/
base.rb:872:in `render_with_no_layout'
/var/lib/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/
layout.rb:262:in `render_without_benchmark'
/var/lib/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/
benchmarking.rb:51:in `render'
/usr/lib/ruby/1.8/benchmark.rb:293:in `measure'
/var/lib/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/
benchmarking.rb:51:in `render'
app/controllers/admin/book_controller.rb:50:in `update'
/var/lib/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/
base.rb:1158:in `send'
/var/lib/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/
base.rb:1158:in `perform_action_without_filters'
/var/lib/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/
filters.rb:697:in `call_filters'
/var/lib/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/
filters.rb:689:in `perform_action_without_benchmark'
/var/lib/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/
benchmarking.rb:68:in `perform_action_without_rescue'
/usr/lib/ruby/1.8/benchmark.rb:293:in `measure'
/var/lib/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/
benchmarking.rb:68:in `perform_action_without_rescue'
/var/lib/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/
rescue.rb:199:in `perform_action_without_caching'
/var/lib/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/
caching.rb:678:in `perform_action'
/var/lib/gems/1.8/gems/activerecord-2.0.2-/lib/active_record/
connection_adapters/abstract/query_cache.rb:33:in `cache'
/var/lib/gems/1.8/gems/activerecord-2.0.2-/lib/active_record/
query_cache.rb:8:in `cache'
/var/lib/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/
caching.rb:677:in `perform_action'
/var/lib/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/
base.rb:524:in `send'
/var/lib/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/
base.rb:524:in `process_without_filters'
/var/lib/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/
filters.rb:685:in `process_without_session_management_support'
/var/lib/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/
session_management.rb:123:in `process_without_test'
/var/lib/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/
test_process.rb:15:in `process'
/var/lib/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/
base.rb:388:in `process'
/var/lib/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/
dispatcher.rb:171:in `handle_request'
/var/lib/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/
dispatcher.rb:115:in `dispatch'
/var/lib/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/
dispatcher.rb:126:in `dispatch_cgi'
/var/lib/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/
dispatcher.rb:9:in `dispatch'
/var/lib/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/
integration.rb:279:in `process'
/var/lib/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/
integration.rb:177:in `post'
test/integration/book_test.rb:81:in `edit_book'
test/integration/book_test.rb:23:in `test_book_administration'
/usr/lib/ruby/1.8/test/unit/testcase.rb:78:in `__send__'
/usr/lib/ruby/1.8/test/unit/testcase.rb:78:in `run'
/var/lib/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/
integration.rb:547:in `run'
/usr/lib/ruby/1.8/test/unit/testsuite.rb:34:in `run'
/usr/lib/ruby/1.8/test/unit/testsuite.rb:33:in `each'
/usr/lib/ruby/1.8/test/unit/testsuite.rb:33:in `run'
/usr/lib/ruby/1.8/test/unit/testsuite.rb:34:in `run'
/usr/lib/ruby/1.8/test/unit/testsuite.rb:33:in `each'
/usr/lib/ruby/1.8/test/unit/testsuite.rb:33:in `run'
/usr/lib/ruby/1.8/test/unit/ui/testrunnermediator.rb:46:in
`run_suite'
/usr/lib/ruby/1.8/test/unit/ui/console/testrunner.rb:67:in
`start_mediator'
/usr/lib/ruby/1.8/test/unit/ui/console/testrunner.rb:41:in `start'
/usr/lib/ruby/1.8/test/unit/ui/testrunnerutilities.rb:29:in `run'
/usr/lib/ruby/1.8/test/unit/autorunner.rb:216:in `run'
/usr/lib/ruby/1.8/test/unit/autorunner.rb:12:in `run'
/usr/lib/ruby/1.8/test/unit.rb:278
test/integration/book_test.rb:96
*************************************************************8
1) Failure:
test_book_administration(BookTest)
[test/integration/book_test.rb:82:in `edit_book'
test/integration/book_test.rb:23:in `test_book_administration'
/var/lib/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/
integration.rb:547:in `run']:
Expected response to be a <:redirect>, but was <500>

1 tests, 15 assertions, 1 failures, 0 errors


I've done some searching, and I'm still not sure why this is happening?

Jarkko Laine

unread,
Aug 12, 2008, 12:16:32 AM8/12/08
to railsec...@googlegroups.com

On 12.8.2008, at 3.56, gumpster wrote:

>
> Hi all,
>
> I'm using Rails 2.0,2 with Ruby 1.8.6 (Gems 0.8.4) and I'm having
> trouble in Chapter 3/4 with integration tests:
>
> ActionView::TemplateError (You have a nil object when you didn't
> expect it!
> You might have expected an instance of Array.
> The error occurred while evaluating nil.map) on line #7 of admin/book/
> _form.html.erb:
> 4: <%= text_field 'book', 'title' %></p>
> 5:
> 6: <p><label for="book_publisher">Publisher</label><br/>
> 7: <%= collection_select :book, :publisher_id, @publishers, :id, :name
> %></p>

@publishers is probably nil, that's what the error message seems to
indicate. How have you set it up in the controller?

//jarkko

--
Jarkko Laine
http://jlaine.net
http://dotherightthing.com
http://www.railsecommerce.com
http://odesign.fi


gumpster

unread,
Aug 12, 2008, 12:40:10 PM8/12/08
to Beginning Ruby on Rails E-Commerce
From publisher_controller:

def list
@publisher_pages, @publishers = paginate :publisher, :per_page =>
10
end

Paginate was installed using classic_pagination.

From "_form.html.erb" :
<p><label for="book_publisher">Publisher</label><br/>
<%= collection_select :book, :publisher_id, @publishers, :id, :name
%></p>

<p><label for="book[author_ids][]">Authors</label><br/>
<%= select_tag 'book[author_ids][]',
options_from_collection_for_select(@authors, :id, :name,
@book.authors.collect{|author| author.id}),
{ :multiple => true, :size => 5 }
%>
</p>

I changed "@publishers" to "Publisher.find(:all)" and
"@authors" to "(Author.find(:all)", after checking
api.rubyonrails.org,
which seems to work...

I'm not sure why?

Jarkko Laine

unread,
Aug 12, 2008, 4:37:57 PM8/12/08
to railsec...@googlegroups.com

On 12.8.2008, at 19.40, gumpster wrote:

>
> From publisher_controller:
>
> def list
> @publisher_pages, @publishers = paginate :publisher, :per_page =>
> 10
> end
>
> Paginate was installed using classic_pagination.
>
> From "_form.html.erb" :
> <p><label for="book_publisher">Publisher</label><br/>
> <%= collection_select :book, :publisher_id, @publishers, :id, :name
> %></p>
>
> <p><label for="book[author_ids][]">Authors</label><br/>
> <%= select_tag 'book[author_ids][]',
> options_from_collection_for_select(@authors, :id, :name,
> @book.authors.collect{|author| author.id}),
> { :multiple => true, :size => 5 }
> %>
> </p>
>
> I changed "@publishers" to "Publisher.find(:all)" and
> "@authors" to "(Author.find(:all)", after checking
> api.rubyonrails.org,
> which seems to work...
>
> I'm not sure why?

IIRC paginator is not part of the Rails core anymore in Rails 2, so
you might need to download some of the pagination plugins like
will_paginate. Not sure why it's not causing an error though, but
instead causing @publishers to be nil. You can check if the current
syntax is different from the one that was current when the book was
written.

You should generally never use calls like Publisher.find(:all) in the
views, btw. They're not part of the view logic and you should avoid
putting actual domain code in the views.

//jarkko

>
>
> On Aug 11, 11:16 pm, Jarkko Laine <jar...@jlaine.net> wrote:
>> On 12.8.2008, at 3.56, gumpster wrote:
>>
>>
>>
>>
>>
>>> Hi all,
>>
>>> I'm using Rails 2.0,2 with Ruby 1.8.6 (Gems 0.8.4) and I'm having
>>> trouble in Chapter 3/4 with integration tests:
>>
>>> ActionView::TemplateError (You have a nil object when you didn't
>>> expect it!
>>> You might have expected an instance of Array.
>>> The error occurred while evaluating nil.map) on line #7 of admin/
>>> book/
>>> _form.html.erb:
>>> 4: <%= text_field 'book', 'title' %></p>
>>> 5:
>>> 6: <p><label for="book_publisher">Publisher</label><br/>
>>> 7: <%= collection_select :book, :publisher_id,
>>> @publishers, :id, :name
>>> %></p>
>>
>> @publishers is probably nil, that's what the error message seems to
>> indicate. How have you set it up in the controller?
>>
>> //jarkko
>>
>> --
>> Jarkko Lainehttp://jlaine.nethttp://dotherightthing.comhttp://www.railsecommerce.comhttp
>> ://odesign.fi
> >

--

Reply all
Reply to author
Forward
0 new messages