Create a filtering function using acts-as-taggable-on

environment

Advance preparation

The following functions are assumed to have been created.

--Posting function --Post details --Install gem --Create table --Introduction of ransack

procedure

--Save tags --Display in view --Implementation of narrowing down

Save tag

Add the following to the model you want to tag.

micropost.rb


acts_as_taggable

Add the following to the strong parameters to save. I will also post create just in case.

microposts_controller.rb


 def create
    @micropost = Micropost.new(micropost_params.merge(user_id: current_user.id))
    if @micropost.save
      redirect_to @micropost, flash:{success: "Posted"}
    else
      render :new
    end
 end

 def show
    @micropost = Micropost.find(params[:id])
 end

 private

 def micropost_params
   params.require(:micropost).permit(:content, :image, :tag_list)
 end

Create and view views

Add the following to your view: Create an input view and a display view.

new.html.slim


= form_with model:  micropost, local: true do |f|
  .form-group
    = f.label :tag
    //,Separated by
    = f.text_field :tag_list, value: @micropost.tag_list.join(","), class: "form-control"
  = f.submit "Send", class: "btn btn-primary"

show.html.slim


table.table.table-hover
    today
      tr
        th = Micropost.human_attribute_name(:tag)
        td
          - @micropost.tag_list.each do |tag|
            = link_to tag, microposts_path(tag_name: tag), class: "micropost_tags__link"

Implementation of filtering function

microposts_controller.rb


  def index
    @q = Micropost.ransack(params[:q])
    if params[:tag_name]
      #Returns information for the same selected post
      @microposts = Micropost.tagged_with(params[:tag_name]).page(params[:page])
    elsif params[:q]
      #Return a list of posts
      @microposts = @q.result(distinct: true).page(params[:page])
    else
      @microposts = Micropost.page(params[:page])
    end
  end

At the end

If you make a mistake, please make an edit request or comment.

Recommended Posts

Create a filtering function using acts-as-taggable-on
Create a login function using Swift's Optional
Create a fortune using Ruby
Create a name input function
Create a login authentication screen using the session function
Create a tomcat project using Eclipse
[Rails] Tag management function (using acts-as-taggable-on)
Create a Java project using Eclipse
[Implementation procedure] Create a user authentication function using sorcery in Rails
Create a web environment quickly using Docker
[Rails] Create an evaluation function using raty.js
[Rails withdrawal] Create a simple withdrawal function with rails
Creating a user authentication function using devise
Create a RESTful API service using Grape
Create a prefectural select bar using active_hash
Create a Privoxy + Tor environment instantly using Docker
Let's create a REST API using WildFly Swarm.
[Rails] How to create a graph using lazy_high_charts
Add a tag function to Rails. Use acts-as-taggable-on
Tag function using acts-as-taggable-on on Devise My memo
Create a Spring Boot application using IntelliJ IDEA
[Rails] I made a draft function using enum
Create a portfolio app using Java and Spring Boot
Create a Java development environment using jenv on Mac
Create an EC site with Rails 5 ⑨ ~ Create a cart function ~
A memorandum when trying to create a GUI using JavaFX
[Rails DM] Let's create a notification function when DM is sent!
Create a tomcat project using Eclipse Pleiades All in One
Create a MOB using the Minecraft Java Mythicmobs plugin | Preparation 1
Search function using [rails] ransack
Creating a calendar using Ruby
[Java] Create a temporary file
Create a VS Code Plugin.
Create a playground with Xcode 12
Implement category function using ancestory
Ajax bookmark function using Rails
About adding a like function
Make a rhombus using Java
How to create a method
Let's create a TODO application in Java 4 Implementation of posting function
How to create a jar file or war file using the jar command
How to create a registration / update function where the table crosses
Create a memo app with Tomcat + JSP + Servlet + MySQL using Eclipse
[Rails 6] How to create a dynamic form input screen using cocoon
Create a static file that expands variables using the ERB class
Let's create a TODO application in Java 6 Implementation of search function
Let's create a TODO application in Java 8 Implementation of editing function
Easy way to create a mapping class when using the API
Oracle Cloud [OCI]: Create a CentOS Stream instance using Compute's cloud-init
[Rails] Implementation of tag function using acts-as-taggable-on and tag input completion function using tag-it
(Ruby on Rails6) Create a function to edit the posted content