Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display specific category based on link pressed | Ruby on Rails

I have a blog that renders every single category with all of their respective sub_categories on the same page. (index view) I have a nav section that I want to utilize to render only a specific subcategories' posts based on the link pressed. I don't know if this is possible with ruby alone, so I thought JQuery might be the way.

blog_categories index.html.erb:

<%= link_to "BLOG", blog_path %> <!-- Will render latest posts. -->

<li role="presentation" class="dropdown">
  <a class="dropdown-toggle" data-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="false">
    NEWS <span class="caret"></span>
  </a>
  <ul class="dropdown-menu">
    <li role="presentation"><a href="#">All News</a></li> <!-- Will render all subcategories that belong to the "news" category -->
    <li role="presentation"><a href="#">Good News</a></li>
    <li role="presentation"><a href="#">Bad News</a></li>
  </ul>
</li>

<li role="presentation" class="dropdown">
  <a class="dropdown-toggle" data-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="false">
    REVIEWS <span class="caret"></span>
  </a>
  <ul class="dropdown-menu">
    <li role="presentation"><a href="#">All Reviews</a></li>
    <li role="presentation"><a href="#">Software</a></li>
    <li role="presentation"><a href="#">Hardware</a></li>
  </ul>
</li>

<% BlogCategory.top_level.find_each do |category| %>
  <% category.sub_categories.find_each do |sub_category| %>
    <% sub_category.posts.find_each do |post| %>

      <%= link_to post do %>
        ...
      <% end %>

    <% end %>
  <% end %>
<% end %>

The blog_categories_controller:

def index
  @category = BlogCategory.find_by_id(params[:id])
  unless @category.nil? # Allows for categories to have nothing in them, eliminating the NoMethodError
    @sub_category = @category.sub_categories.first
    @posts = @subcategory.posts
  end
end


private

  def cat_params
    params.require(:blog_category).permit(:name, :parent_id, :sub_category)
  end

My sub_categories are related to main categories through a parent_id column in the blog_categories table via self-referential relation.

I've read a bit about active record querying on guides.rubyonrails.org and seen something about conditions such as Client.where("orders_count = ?", params[:orders]) Could this be used?

If this isn't the proper way of using the HTML dropdowns to iterate through BlogCategory, I would love to know what way would be best to handle this problem, thank you!

like image 822
Jake Avatar asked Jun 05 '18 16:06

Jake


1 Answers

Looking at your code and question, it seems you want to show all the categories, their sub categories and the posts in each sub category on your index page. I am assuming you have a finite number of posts, else you might want to limit it to n (5?) number of posts per sub category. If so, you can have the following controller:

def index
  @categories = BlogCategory.top_level.includes(sub_categories: :posts)
end

Then in the view, you can easily access @categories, @categories[i].sub_categories and @categories[i].sub_categories[j].posts without any extra queries being triggered from the view. Something on the lines of:

<% @categories.each do |category| %>
  <li role="presentation" class="dropdown">
  <a class="dropdown-toggle" data-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="false">
    <%= category.name %> <span class="caret"></span>
  </a>
  <ul class="dropdown-menu">
    <% category.sub_categories.each do |sub_category| %>
      <li role="presentation"><a href="#"><%= sub_category.name %></a></li>
      <!-- And if you want to show the posts here, then loop through sub_category.posts -->
    <% end %>
  </ul>
  </li>
<% end %>

You can use jquery to show and hide relevant sub categories and posts depending on what the user clicks. Here's an example: http://api.jquery.com/show/

like image 113
amit_saxena Avatar answered Sep 28 '22 04:09

amit_saxena