2014-01-25 118 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给他们的层次结构

与标签不同,我看到类别主要是固定的,这意味着你可以做你的希望与他们在后端,向用户展示结构化的选择