2014-01-26 102 views
0

我一直在尋找所有的網絡在這過去三個小時的答案,所以我會發佈一個問題。我正在嘗試創建一些基本的論壇軟件,但我遇到了與我的關係問題。這是錯誤我得到Rails 4協會:undefined方法

undefined method `forums' for # <ActiveRecord::Relation::ActiveRecord_Relation_Category:0x3706cb0> 

我的類別/索引文件

<% for category in @category %> 
      <div class="panel panel-success"> 
       <div class="panel-heading"> 
       <h3 class="panel-title"><%= link_to category.name, category_path(category.id) %></h3> 
       <h4><%= category.description %> </h4> 
       </div> 
       <% for forum in @category.forums %> 
        <div class="panel-body"> 
        <h4><span class="glyphicon glyphicon-tree-conifer"></span><%= link_to forum.name, forum_path(forum.id) %> </h4> 
        </div> 
        <hr /> 
       <% end %> 
      </div> 
      <% end %> 

分類控制器

class CategoriesController < ApplicationController 

     def index 
     @category = Category.all 
     end 

     def show 
     @category = Category.find(params[:id]) 
     end 

end 

論壇控制器

class ForumsController < ApplicationController 

    def show 
    @forum = Forum.find(params[:id]) 
    end 
end 

的分類模型

class Category < ActiveRecord::Base 
    has_many :forums, dependent: :destroy 
end 

論壇車型

class Forum < ActiveRecord::Base 
    belongs_to :category 
    has_many :threads, dependent: :destroy 
end 

和我的路線

ForumName::Application.routes.draw do 
    root 'static_pages#home' 

    resources :users 
    resources :sessions, only: [:new, :create, :destroy] 
    resources :categories, :path => "forum" 
    resources :forums 
    resources :topics 
    resources :posts 

    match '/signup', to: 'users#new',   via: 'get' 
    match '/signin', to: 'sessions#new',  via: 'get' 
    match '/signout', to: 'sessions#destroy', via: 'delete' 

end 

希望我提供的,如果別的要求,我會更新帖子足夠的信息。通常我可以使用谷歌工作這些東西,但這真的讓我難住。提前感謝。在行動CategoriesController#指數

回答

0

問題

應改爲 @categories = Category.all

因爲Category.all期望得到幾類,而不是一個

你也應該改變的類別/索引文件

<% for category in @categories %> 
     <div class="panel panel-success"> 
      <div class="panel-heading"> 
      <h3 class="panel-title"><%= link_to category.name, category_path(category.id) %></h3> 
      <h4><%= category.description %> </h4> 
      </div> 
      <% for forum in category.forums %> 
       <div class="panel-body"> 
       <h4><span class="glyphicon glyphicon-tree-conifer"></span><%= link_to forum.name, forum_path(forum.id) %> </h4> 
       </div> 
       <hr /> 
      <% end %> 
     </div> 
     <% end %> 
+0

非常感謝!我不知道爲什麼我改變它之前,我在我的控制器中使用類別。所以我的問題整個時間寫作@ category.forums。 –