Day 74 : #100DaysofCode - Finished Adding Categories

sincerelybrittany

Brittany

Posted on August 14, 2020

Day 74 : #100DaysofCode - Finished Adding Categories

I was pretty stressed out setting up my categories today. The first part was easy, create a model and create a migration that will add the id to the post model.

Create Model

rails g model category name:string desc:text 
Enter fullscreen mode Exit fullscreen mode

Add Columns To Posts Migration

rails generate migration AddColumnsToPosts category_id:integer
Enter fullscreen mode Exit fullscreen mode

I realized I want to show all categories and show post based off a category selected so I needed a Category controller:

Category Controller

rails g controller Category
Enter fullscreen mode Exit fullscreen mode

Now I had to set up my models to reflect the following:

Post Model

class Post < ApplicationRecord
   belongs_to :category
end
Enter fullscreen mode Exit fullscreen mode

Category Model

class Category < ApplicationRecord
    has_many :posts
end
Enter fullscreen mode Exit fullscreen mode

The next part took some google searching, how can I display all of the categories in a drop down? When I select a category in the dropdown, how will I display the post associated with that category?

The Select Tag and options_from_collection_for_select is how!

I added the following to my categories index view page:

<%= form_tag posts_path, method: :get do %>
    <%= select_tag "category_id", options_from_collection_for_select(Category.all, "id", "name"), {:include_blank => 'All Categories'} %>
    <%= submit_tag "Search", name: nil, :class => "myButton" %>
<% end %>
Enter fullscreen mode Exit fullscreen mode

The breakdown/what I think I understand.

select_tag(name, option_tags = nil, options = {})

form_tag posts_path, method: :get do makes a get request to the posts_path

select_tag "category_id" is the name that will be passed on in the params.

options_from_collection_for_select(Category.all, "id", "name") creates a dropdown selection box for all categories, passes its id for the params, and displays the name in the dropdown list.

{:include_blank => 'All Categories'} displays "Search By Category" in the dropdown before anything is selected, it is like a placeholder.

The Post Controller

This caused me a bit of a headache, I struggled with trying to figure out WHY my dropdown failed to show me ALL my post again when I pressed "All Categories" from the {:include_blank => 'All Categories'}. I figured out with a byebug that when I search for that, my category_id was empty! So I added if params[:category_id] && !params[:category_id].empty? to my controller to fix that issue.

class PostsController < ApplicationController
  before_action :authenticate_user, except: [:index]
    def index
     if params[:category_id] && !params[:category_id].empty?
            @category = params[:category_id]
            @posts = Post.where(category_id: @category)
            render 'index'
      else
        @posts = Post.all
        @categories = Category.all
      end
    end
Enter fullscreen mode Exit fullscreen mode

The above checks to see if the params hold a category_id and makes sure that the category_id is NOT empty. If those two things are true then it will search through all posts where the category_id is the one that matches the params.

If the category_id is empty or if no one has searched by category, the index page will display all of the posts and give the dropdown form access to all of the categories still.

This was a process but I definitely learned a lot today.

As always, thanks for reading!

Sincerely,
Brittany

Song of the day:

πŸ’– πŸ’ͺ πŸ™… 🚩
sincerelybrittany
Brittany

Posted on August 14, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related