rendering collections with partials

i’m trying to follow these instructions:

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

in my index.html.erb

    <%= render @parent_categories %>
in my _categories.html.erb
  • <%= category.name %>
      <%= render category.categories %>
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 # 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

Colin Law <clanlaw@gmail.com> writes:

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

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 Active Record Query Interface — Ruby on Rails Guides

2. partials for collections

See Layouts and Rendering in Rails — Ruby on Rails Guides 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.

thanks tamara

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

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 ?

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 <fugee279@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.