List the contents of categories created with Active Hash

Introduction

Thing you want to do

--List the contents associated with each category created by Active Hash

procedure

--Set up routing --Make a link --Write the action in the controller --Create the corresponding view

As a prerequisite, it is assumed that the installation of Active Hash and the settings around the model / DB have been completed.

Set up routing

Since the Item model has category_id, set the routing as follows

routes.rb


get '/items/category/:id', to: "items#category"

Make a link

Make a link.

html.erb


link_to 'hoge', '/items/category/1'
link_to 'fuga', '/items/category/2'
link_to 'piyo', '/items/category/3'

By the way, the Category model is set like this

models/category.rb


class Category < ActiveHash::Base
  self.data = [
    { id: 1, name: 'hoge' },
    { id: 2, name: 'fuga' },
    { id: 3, name: 'piyo' }
  ]
end

Write the action in the controller

category Defines an action.

items_controller.rb


def category
  @item = Item.find_by(category_id: params[:id])
  @items = Item.where(category_id: params[:id]).order('created_at DESC')
end

@Item is defined to display the category name on the category list page, and @items is defined to expand with each.

Make a corresponding view

Create category.html.erb under items.

ruby:items/category.html.erb


#Display of category name
  @item.category.name
#Expand the contents of the category
  @items.each do |item|
~ Omitted ~
  end

in conclusion

I think I could write it quite easily by passing each id with link_to. Please let me know if there is a better way!

✔︎

Recommended Posts

List the contents of categories created with Active Hash
Category search with ransack using categories created with Active hash
Check the contents of params with pry
Format the contents of LocalDate with DateTimeFormatter
Verify the contents of the argument object with Mockito
Overwrite the contents of config with Spring-boot + JUnit5
Utilization of Active Hash
Pitfalls of Active Hash
Test the contents of an Excel file with JUnit
Implemented pull-down in the original app with Active Hash
Delete all the contents of the list page [Ruby on Rails]
[Ruby] How to retrieve the contents of a double hash
About the behavior of ruby Hash # ==
[Java] Delete the elements of List
[Rails] Check the contents of the object
Replace the contents of the Jar file
Official logo list of the service
[Ruby] Display the contents of variables
Examine the contents of the WAR file generated by the project created by Spring Initializr
[Java] Get MimeType from the contents of the file with Apathce Tika [Kotlin]
After all I wanted to preview the contents of mysql with Docker ...
Check the contents of the Java certificate store
Memo: [Java] Check the contents of the directory
Folding and unfolding the contents of the Recyclerview
[Ruby] Cut off the contents of twitter-ads
How to sort the List of SelectItem
Check the processing contents with [rails] binding.pry
About the treatment of BigDecimal (with reflection)
The contents of the data saved by CarrierWave.
[Java] Contents of Collection interface and List interface
[No.004] Corrected the order list screen of the orderer
JAVA: jar, aar, view the contents of the file
Add empty data to the top of the list
Customize how to divide the contents of Recyclerview
Ominous odor list suggesting the possibility of refactoring
Manage the version of Ruby itself with rbenv
The story of tuning android apps with libGDX
I want to var_dump the contents of the intent
Calculate the similarity score of strings with JAVA
Prepare the environment of CentOS 8 with Sakura VPS
Specify the default value with @Builder of Lombok
Measure the distance of the maze with breadth-first search
I checked the number of taxis with Ruby
Speed comparison when the value side of Hash wants to retrieve all with array
[Docker] How to see the contents of Volumes. Start a container with root privileges.
[Personal memorandum] Button layout created with javafx in Eclipse (in the case of HBox / VBox)