awesome_nested_set views ... help me troubleshoot

22 views
Skip to first unread message

fugee ohu

unread,
Nov 25, 2015, 5:00:52 PM11/25/15
to Ruby on Rails: Talk
catalog.html.erb fails to display the listings

In catalog.html.erb
<div>
 <table>
 <% @category.descendants.each do |category| %>
  <% category.listings.each do |listing| %>
   <%= listing.title %>
  <% end %>
 <% end %>
 </table>
</div>

In my routes.rb
  get '/store/catalog/:id' => 'store#catalog'

In my store_controller.rb
  def catalog
   @category=Category.find(params[:id])
  end

In category.rb
class Category < ActiveRecord::Base
 acts_as_nested_set
 has_many :listings
end

In listing.rb
class Listing < ActiveRecord::Base
 belongs_to :categories
end


Colin Law

unread,
Nov 25, 2015, 5:13:40 PM11/25/15
to Ruby on Rails: Talk
On 25 November 2015 at 22:00, fugee ohu <fuge...@gmail.com> wrote:
> catalog.html.erb fails to display the listings
>
> In catalog.html.erb
> <div>
> <table>
> <% @category.descendants.each do |category| %>
> <% category.listings.each do |listing| %>
> <%= listing.title %>
> <% end %>
> <% end %>
> </table>
> </div>

If my suggestion in the other thread does not help then you can debug
this by using the logger. You could make the above look like
<table>
<% logger.info "descendants count = #{@category.descendants.count}" %>
<% @category.descendants.each do |category| %>
<% logger.info "listings count = #{category.listings.count}" %>
<% category.listings.each do |listing| %>
<% logger.info listing.inspect %>
<%= listing.title %>

Then you can look in the log to get an idea of what is happening.
Perhaps the category has no descendants or they have no listings.

See below also


>
> In my routes.rb
> get '/store/catalog/:id' => 'store#catalog'
>
> In my store_controller.rb
> def catalog
> @category=Category.find(params[:id])
> end
>
> In category.rb
> class Category < ActiveRecord::Base
> acts_as_nested_set
> has_many :listings
> end
>
> In listing.rb
> class Listing < ActiveRecord::Base
> belongs_to :categories

That should be category not categories, though I am surprised that did
not give an error. I hope you are copy/pasting here not re-typing.

Colin

fugee ohu

unread,
Nov 25, 2015, 5:29:13 PM11/25/15
to Ruby on Rails: Talk
  [1m [36m (50.3ms) [0m  [1mSELECT COUNT(*) FROM `categories` WHERE (`categories`.`lft` >= 0) AND (`categories`.`lft` < 0) AND (`categories`.`id` != 1) [0m
descendants count = 0

i created the lft field for added functionality but never populated it i dunno where it's supposed to get it's value from or even what it is

<div>

 <table>
 <% logger.info "descendants count = #{@category.descendants.count}" %>
 <% @category.descendants.each do |category| %>
  <% category.listings.each do |listing| %>
   <% logger.info listing.inspect %>
   <tr><td><%= listing.title %>

fugee ohu

unread,
Nov 25, 2015, 5:38:37 PM11/25/15
to Ruby on Rails: Talk
the problem is i entered a few rows to listings manually inserting just fields i thought i'd need before i implemented awesome_nested_set

fugee ohu

unread,
Nov 25, 2015, 8:07:08 PM11/25/15
to Ruby on Rails: Talk
I deleted all the rows in the categories table and re-created the categories following the instructions in awesome_nested_set cheat sheet but still only a blank page loads Here's the latest from development.log ~ thanks

Started GET "/store/catalog/1" for 127.0.0.1 at 2015-11-25 20:01:20 -0500
Processing by StoreController#catalog as HTML
  Parameters: {"id"=>"1"}
  [1m [35mCategory Load (0.2ms) [0m  SELECT  `categories`.* FROM `categories` WHERE `categories`.`id` = 1 LIMIT 1
  [1m [36mCACHE (0.0ms) [0m  [1mSELECT  `categories`.* FROM `categories` WHERE `categories`.`id` = 1 LIMIT 1 [0m  [["id", "1"]]
  [1m [35m (0.4ms) [0m  SELECT COUNT(*) FROM `categories` WHERE (`categories`.`lft` >= 1) AND (`categories`.`lft` < 8) AND (`categories`.`id` != 1)
descendants count = 3
  [1m [36mCategory Load (0.2ms) [0m  [1mSELECT `categories`.* FROM `categories` WHERE (`categories`.`lft` >= 1) AND (`categories`.`lft` < 8) AND (`categories`.`id` != 1)  ORDER BY `categories`.`lft` [0m
  [1m [35mListing Load (0.2ms) [0m  SELECT `listings`.* FROM `listings` WHERE `listings`.`category_id` = 2
  [1m [36mListing Load (0.2ms) [0m  [1mSELECT `listings`.* FROM `listings` WHERE `listings`.`category_id` = 3 [0m
  [1m [35mListing Load (0.2ms) [0m  SELECT `listings`.* FROM `listings` WHERE `listings`.`category_id` = 4
  Rendered store/catalog.html.erb within layouts/store (7.8ms)
  [1m [36mAdmin Load (0.2ms) [0m  [1mSELECT  `admins`.* FROM `admins` WHERE `admins`.`id` = 1  ORDER BY `admins`.`id` ASC LIMIT 1 [0m
Completed 200 OK in 113ms (Views: 109.3ms | ActiveRecord: 1.6ms)

fugee ohu

unread,
Nov 25, 2015, 8:29:42 PM11/25/15
to Ruby on Rails: Talk
Oh never mind, the category_id was blank in the listing The main <div> works, on to the side, thanks
Reply all
Reply to author
Forward
0 new messages