Rails hashtag search implementation

This time, I implemented the hashtag function in rails. https://glodia.jp/blog/3936/ I referred to the site of.

Completion drawing If you add a tag when listing an item

スクリーンショット 2020-09-07 18.44.13.png

You can check the hashtag that you can follow the link at the time of posting details, スクリーンショット 2020-09-07 18.45.37.png

Products with the same tag are lined up at the end of the link スクリーンショット 2020-09-07 18.57.37.png

Implementation method 1 Product table items and tag table tags are connected by an intermediate table 2 Write a description in the helper to link the tag name.

items_helper.rb


def render_with_hashtags(tag_name, tag_id)
tag_name.gsub(/[##][\w\p{Han}Ah-Gae-゚]+/){|word| link_to word, "/item/hashtag/#{word.delete("#")}?tag_id=#{tag_id}"}.html_safe
end

The name of the tag is passed as the first argument, and the id of the tag stored in the intermediate table is passed as the second argument. By replacing it with url with the gsub method and adding the html_safe method, you can avoid escaping and you can follow the link in the form of "# name" in the view. In the description of? tag_id = # {tag_id}, put the id of the tag in the link

3 Routing implementation

routes.rb


 get '/item/hashtag/:name', to: "items#hashtag"

I think it's best to nest inside the item controller

4 Controller implementation

items_controller.rb


def hashtag
  @tag = Tag.find(params[:tag_id])
  @items = @tag.items
end

Pass the tag_id prepared in the link in 2 with params and search for the corresponding tag Put the product with the corresponding tag in the variable

5 Implement the view using @items for each method as when creating the post details list

done!

Recommended Posts

Rails hashtag search implementation
Rails search function implementation
Rails fuzzy search function implementation
[Rails 6] Implementation of search function
[Rails] Implementation of search function using gem's ransack
Rails6 countdown timer implementation
[rails] gem'payjp' implementation procedure
Rails Action Text implementation
[Rails] Implement search function
Implementation of search function
[Rails] Implemented hashtag function
Search function [copype implementation]
Rails implementation of ajax removal
[Rails] Implement User search function
Search function using [rails] ransack
Implementation of sequential search function
Use ransack Search function implementation
[Rails] Creating a search box
[Rails] Implementation of category function
Login function implementation with rails
[Rails] Implementation of tutorial function
[Rails] Implementation of like function
[Rails 6] Pagination function implementation (kaminari)
[Rails] Implementation of user logic deletion
[Rails] Implementation of CSV import function
[Rails] Asynchronous implementation of like function
[Rails] Keyword search in multiple tables
Add a search function in Rails.
[rails] Login screen implementation in devise
[Rails] Implementation of image preview function
Implemented hashtag search like Instagram and Twitter in Rails (no gem)
[Rails] About implementation of like function
[Rails] Implementation of user withdrawal function
[Rails] Implementation of CSV export function
[Rails] Implementation of many-to-many category functions
[Rails] gem ancestry category function implementation
[Ruby on Rails] Comment function implementation
[Rails 6] Like function (synchronous → asynchronous) implementation
[Rails] Comment function implementation procedure memo
Timeless search with Rails + JavaScript (jQuery)
[Ruby on Rails] Search function (not selected)
How to implement search functionality in Rails
[Ruby on Rails] Follow function implementation: Bidirectional
Rails [For beginners] Implementation of comment function
Rails Basic CRUD function implementation procedure scaffold
[Rails 6] Implementation of SNS (Twitter) sharing function
[Vue.js] Implementation of menu function Implementation version rails6
[Rails] Method summary for conversion / verification / search
[Rails] Book search with Amazon PA API
[Ruby on rails] Implementation of like function
[Java] New Yahoo! Product Search API Implementation
[Rails] Implementation of validation that maintains uniqueness
[Vue.js] Implementation of menu function Vue.js introduction rails6
[Rails] Set validation for the search function using Rakuten API (from the implementation of Rakuten API)