Undefined method `model_name in partial passing an Array as a collection

In the controller , I am passing an Array as a collection
@objects = [“item1”, “item2”, “item3”, “item4”]

in the view I wrote
%table#objects
= render :partial => ‘object’, :collection => @objects

and in my partial _object.html
= content_tag_for(:tr, @object) do
%td= @object
%td= link_to…

but I got an error : undefined method `model_name’ for NilClass:Class
for the #object in the content_tag

@object is not not a class… (yet) I just to want to test on an
Array of string … anyway of doing such thing ?

thanks for your feedback

On Mar 13, 11:42am, Erwin [email protected] wrote:

%td= link_to........

but I got an error : undefined method `model_name’ for NilClass:Class
for the #object in the content_tag

@object is not not a class… (yet) I just to want to test on an
Array of string … anyway of doing such thing ?

@object isn’t set - you should be using object (since that’s what
render will be filling with the items from @objects

Fred

Thanks fred… I tried with object but same error… it seems
model_name_from_record_or_class is needed when using collection
I may have to change the partial rendering … ?

– trace
activemodel (3.0.4) lib/active_model/naming.rb:94:in
model_name_from_record_or_class' activemodel (3.0.4) lib/active_model/naming.rb:81:insingular’
actionpack (3.0.4) lib/action_controller/record_identifier.rb:49:in
dom_class' actionpack (3.0.4) lib/action_view/helpers/record_tag_helper.rb:58:incontent_tag_for’
app/views/admin/remote_lockers/_object.html.haml:1:in
_app_views_admin_remote_lockers__object_html_haml__3334574884651529235_2179828200_2175116202532159367' actionpack (3.0.4) lib/action_view/template.rb:135:inblock in
render’
activesupport (3.0.4) lib/active_support/notifications.rb:54:in
instrument' actionpack (3.0.4) lib/action_view/template.rb:127:inrender’
actionpack (3.0.4) lib/action_view/render/partials.rb:300:in block in collection_with_template' actionpack (3.0.4) lib/action_view/render/partials.rb:297:ineach’
actionpack (3.0.4) lib/action_view/render/partials.rb:297:in
collection_with_template' actionpack (3.0.4) lib/action_view/render/partials.rb:280:inrender_collection’

On Mar 13, 1:28pm, Erwin [email protected] wrote:

%td= item

this time , the error is : undefined method `model_name’ for
String:Class

I believe there is something wrong with the content_tag_for…

Oops, hadn’t spotted that you were using content_tag for as opposed to
content_tag. The entire point of content_tag_for is that you give it
an activemodel like object and it will set the dom id & css class
appropriately.
If you don’t have such objects (eg you have strings instead), then
don’t use content_tag_for. content_tag might be what you’re looking
for.

Fred

thanks you confirm it … I noticed it as I change my test code using
the array as the collection …
it’s fine now … using content_tag obvious thing doesn’t appears at
first glance !!

to avoid being confused with ‘object’ litteral … I modified the test
code as following :

#lockers
%table#items
= render :partial => ‘item’, :collection => [‘foo’, ‘bar’, ‘baz’]

and the _item partial

= content_tag_for(:tr, item) do
%td= item

this time , the error is : undefined method `model_name’ for
String:Class

I believe there is something wrong with the content_tag_for…

On Sun, Mar 13, 2011 at 4:42 AM, Erwin [email protected] wrote:

   %td= @object
   %td= link_to........

you are using the partial in the wrong way.

when you use :collection => @objects

it will pass each element of the collection into the partial and create
‘object’ which is created based
on the name of the partial ‘_name’. the same thing happens with

:partial ‘thing’, :object => Thing.new

_thing.haml
= thing # this is the :object, based on the name

also call your partial .haml if it is haml.


make haste slowly
festina lente \

mobile +1_415_632_6001
[email protected] [email protected]
http://robotarmyma.de

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs