rendering collections with partials

363 views
Skip to first unread message

fugee ohu

unread,
Nov 21, 2015, 1:50:09 PM11/21/15
to Ruby on Rails: Talk
i'm trying to follow these instructions:

in my categories_controller.rb
def index @parent_categories = Category.without_parents

in my index.html.erb
<ul> <%= render @parent_categories %> </ul>

in my _categories.html.erb
<ul> <li> <strong><%= category.name %></strong> <ul> <%= render category.categories %> </ul> </li> </ul>

but this doesn't work i get unknown method wihtout_parents and couldn't figure out what without_parents was supposed to be so, i tried changing the line in the controller to @parent_categories = Category.where("parent_id=0") and it was on to the next error, now i get undefined method `categories' for #<Category:0xb1604e2c> My understanding is that in the statement, category is the partial view and categories means the entire collection, so then why doesn't rails recognize the meaning of categories as the entire collection and what's the story with "without_parents" ? ~ thanks, fugee

Colin Law

unread,
Nov 21, 2015, 4:44:02 PM11/21/15
to Ruby on Rails: Talk
I am afraid your ideas are so horribly mixed up it is difficult to know where to start.  I think the instructions you are following must be rather poor.  I suggest instead you start by working right through railstutorial.org (which is free to use online) which will show you the basics of Rails in a way which most can follow.

Colin
 

Tamara Temple

unread,
Nov 22, 2015, 9:48:49 AM11/22/15
to rubyonra...@googlegroups.com
As ever, Colin's advice is correct. Do work through a full tutorial,
maybe two or three times, even.

Notwithstanding that, there are a couple things you're trying to do
here.

1. defined scopes

the `.without_parents` would be a *scope* you have to define as a Class
Method on your Category model. It's essentially like so:

class Category < ActiveRecord::Base
# ...

scope :without_parents, ->() { where( parent_id: 0 ) }

# ...
end

See http://guides.rubyonrails.org/active_record_querying.html#scopes


2. partials for collections


See http://guides.rubyonrails.org/layouts_and_rendering.html#using-partials
subsection 3.4.5 Rendering Collections. When you have a collection of
categories, as you do with `@parent_categories`, you name the partial
`category.html.erb` (NOTE SINGULAR) and the variable available inside
that partial is also `category` (again singular) and it contains ONE
category from `@parent_categories` at a time.


--
Tamara Temple
tam...@gmail.com
http://www.tamouse.org

fugee ohu

unread,
Nov 22, 2015, 11:20:51 PM11/22/15
to Ruby on Rails: Talk
thanks tamara

fugee ohu

unread,
Nov 22, 2015, 11:59:31 PM11/22/15
to Ruby on Rails: Talk
according to what you say i don't see what's wrong with my _category.html.erb it does use the singular syntax "category.name" and "category.categories" i don't get what you're trying to point out in my partial ~ fugee

fugee ohu

unread,
Nov 23, 2015, 10:05:23 AM11/23/15
to Ruby on Rails: Talk
after defining wihtout_parent in the model i reloaded the page and got this error for line 2 of index.html.erb
  <%= render @parent_categories %>
error:
'nil' is not an ActiveModel-compatible object. It must implement :to_partial_path.

was <%= render @parent_categories %> supposed to call the _category.html.erb partial ?

Tamara Temple

unread,
Nov 25, 2015, 12:39:01 AM11/25/15
to rubyonra...@googlegroups.com

> On Saturday, November 21, 2015 at 4:44:02 PM UTC-5, Colin Law wrote:
>>
>> On 21 November 2015 at 18:50, fugee ohu <fuge...@gmail.com <javascript:>>
>> wrote:
>>
>>> i'm trying to follow these instructions:
>>>
>>> in my categories_controller.rb
>>> def index @parent_categories = Category.without_parents
>>>
>>> in my index.html.erb
>>> <ul> <%= render @parent_categories %> </ul>
>>> in my _categories.html.erb
>>> <ul> <li> <strong><%= category.name %></strong> <ul> <%= render
>>> category.categories %> </ul> </li> </ul>
>>> but this doesn't work i get unknown method wihtout_parents and couldn't
>>> figure out what without_parents was supposed to be so, i tried changing the
>>> line in the controller to @parent_categories =
>>> Category.where("parent_id=0") and it was on to the next error, now i get
>>> undefined method `categories' for #<Category:0xb1604e2c> My understanding
>>> is that in the statement, category is the partial view and categories means
>>> the entire collection, so then why doesn't rails recognize the meaning of
>>> categories as the entire collection and what's the story with
>>> "without_parents" ? ~ thanks, fugee
>>>
>>
>>
>> I am afraid your ideas are so horribly mixed up it is difficult to know
>> where to start. I think the instructions you are following must be rather
>> poor. I suggest instead you start by working right through
>> railstutorial.org (which is free to use online) which will show you the
>> basics of Rails in a way which most can follow.
>>
>> Colin
>>
>>
>>

Cutting this from the top of the message and putting it at the bottom:

fugee ohu <fuge...@gmail.com> writes:

> after defining wihtout_parent in the model i reloaded the page and got this
> error for line 2 of index.html.erb
>
> <%= render @parent_categories %>
> error:
>
> 'nil' is not an ActiveModel-compatible object. It must implement :to_partial_path.
>
> was <%= render @parent_categories %> supposed to call the
> _category.html.erb partial ?

What's the content of @parent_categories at that point?
Insert `<%= debug @parent_categories %>` above the render line and see
what the view sees.

I wrote a small test app to deal with self-nested objects, so I know
this sort of thing does work. As we only get to see what you show us,
perhaps you could give us a link to your public repository. Or even put
up a gist with all the applicable parts.

Or do what I did and write a tiny example application focusing just on
just this part and see if you can get it to work, and provide a link to
the app's source for us to see.
Reply all
Reply to author
Forward
0 new messages