2014-01-25 115 views
0

我需要有人引導我爲我的帖子模型生成類別。我想在頂部有一個主導航欄,在側欄部分有多個子菜單的所有類別。爲帖子創建類別

我閱讀了多篇文章,發現我可以爲我的類別生成多個模型或使用標籤。我已經創建了標記模型,但不知道如何使用它創建樹結構。

-----編輯----------

我使用行爲-AS-加標籤,創業板和我的Post模型創建的標籤已經嘗試過。但不知道如何使用它來創建導航和側邊欄的類別。

///// -----編輯2 --------- ///

視圖/ application.html.erb

<ul> 
    <% Tag.roots.each do |tag| %> 
    <li><%= link_to tag.name, tag_path(tag) %></li> 
    <% end %> 
</ul> 

這形成了一個列表的標籤根節點。當我點擊標籤之一,我得到以下錯誤:

Started GET "/tags/1" for 127.0.0.1 at 2014-01-25 01:23:48 -0500 
Processing by PostsController#index as HTML 
    Parameters: {"tag"=>"1"} 
    Tag Load (0.3ms) SELECT "tags".* FROM "tags" WHERE "tags"."name" = '1' LIMIT 1 
Completed 404 Not Found in 3ms 

ActiveRecord::RecordNotFound (ActiveRecord::RecordNotFound): 
    app/models/post.rb:6:in `tagged_with' 
    app/controllers/posts_controller.rb:23:in `index' 

這是我已經把對路線: 的routes.rb

get 'tags/:tag', to: 'posts#index', as: :tag 

我還包括posts_controller.rb我:

class PostsController < ApplicationController 


    def new 
    @post = Post.new 
    end 

    def create 
    @post = Post.new(post_params) 
    if @post.save(params[:post].permit(:title, :body, :tag_list)) 
     redirect_to @post 
    else 
     render 'new' 
    end 
    end 

    def show 
    @post = Post.find(params[:id]) 
    end 

    def index 
    if params[:tag] 
     @posts = Post.tagged_with(params[:tag]) 
    else 
     @posts = Post.all 
    end 
    end 

    def update 
    @post = Post.find(params[:id]) 

    if @post.update(params[:post].permit(:title, :body, :tag_list)) 
     redirect_to @post 
    else 
     render 'edit' 
    end 
    end 

    def edit 
    @post = Post.find(params[:id]) 
    end 

    def destroy 
    @post = Post.find(params[:id]) 
    @post.destroy 

    redirect_to posts_path 
    end 

    private 
    def post_params 
     params.require(:post).permit(:title, :body, :tag_list) 
    end 
end 
+2

https://github.com/mbleigh/acts-as-taggable-on是你最好的朋友。 –

+0

@ITNinja我已經安裝了act-as-taggable-on並將其實現到我的Post模型,以便用戶可以輸入和創建標籤。但我不確定如何使用這些標籤創建類別。另外,acts-as-taggable-on的README頁面解釋了創建tag_list的方式,但不知道我可以在哪裏陳述這個來生成tag_list供我使用。 – wag0325

+1

好吧,你可以做這樣的事情。您將標籤附加到具有給定類別的帖子中。然後,爲了獲得該類別的所有項目,您可以使用該給定標籤搜索所有帖子。爲了創建側邊欄,您需要創建邏輯來檢查給定類別的查詢參數(添加到link_to的查詢參數)。在邏輯中,您將返回具有該特定標籤的帖子。 –

回答

0

IMO你感到困惑與2個不同的問題:

  1. Post與許多Categories
  2. 分類層次

帖子

如果你想你的崗位上有很多種類,你可以只使用一個HABTM協會:

#app/models/post.rb 
Class Post < ActiveRecord::Base 
    has_and_belongs_to_many :categories 
end 

#app/models/category.rb 
Class Category < ActiveRecord::Base 
    has_and_belongs_to_many :posts 
end 

你需要創建一個categories_posts表像這樣(Rails 3 has_and_belongs_to_many migration):

#db/migrate/your_migration.rb 
create_table :categories_posts, :id => false do |t| 
    t.references :categories 
    t.references :posts 
end 

這將讓你的任何post與許多categories只要你想關聯。有許多方法可以做到這一點,但最簡單的就是簡單地使用<< ActiveRecord function

#app/controllers/posts_controller.rb 
def add_category 
    @post = Post.find(params[:id]) 
    @post.categories << Category.find(params[:category_id]) 
end 

層次

因爲你有你categories單獨您posts,你會能夠使用寶石,如ancestry給他們的層次結構

與標籤不同,我看到類別主要是固定的,這意味着你可以做你的希望與他們在後端,向用戶展示結構化的選擇