I need someone to guide me in generating Categories for my Post model. I want to have a main navigation bar at the top and all the categories with multiple submenus in the sidebar section.
I read multiple posts and found out I could either generate multiple models for my categories or use tags. I've already created tag model but not sure how I can create a tree structure using this.
-----edit----------
I've already tried using Acts-As-Taggable-On Gem and created tags for my Post model. But not sure how I can use this to create a categories for navigation and sidebar.
/////-----Edit 2 ---------///
views/application.html.erb
<ul>
<% Tag.roots.each do |tag| %>
<li><%= link_to tag.name, tag_path(tag) %></li>
<% end %>
</ul>
This forms a list of Tag root nodes. When I click one of the tag, I get following error:
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'
This is what I've put for routes: routes.rb
get 'tags/:tag', to: 'posts#index', as: :tag
I'm also including 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