Act_as_Taggable 不返回所选标签上的元素

问题描述

我有一个简单的簿记 rails 应用程序,它实现了 Act_as_Taggable_on,它在我的索引上显示所有创建的标签,但是当我选择一个时,它返回所有索引,而不仅仅是与该标签关联的元素;在这个问题上需要一些帮助,请。 我的代码是:

routes.rb

Rails.application.routes.draw do
  devise_for :users
  root to: 'pages#home'
  # For details on the DSL available within this file,see https://guides.rubyonrails.org/routing.html
   get '/tagged',to: "books#tagged",as: :tagged
   #get 'tagged' => 'books#tagged',:as => 'tagged'

  resources :users do
    resources :books do
      resources :loans,only: [:index,:new,:create,:destroy]
    end
  end
end

books_controller

class BooksController < ApplicationController
  before_action :authenticate_user!
  before_action :skip_authorization,only: [:tagged]
  
  def index
    unless params[:term].present?
      @books = policy_scope(Book)
    else
      @books = policy_scope(Book)
      @books = Book.search_by_full_name(params[:term])
    end
  end

  def show
    @book = Book.find(params[:id])
    authorize @book
  end

  def new
    @user = User.find(params[:user_id])
    @book = Book.new
    authorize @book
  end

  def create
    @user = User.find(params[:user_id])
    @book = Book.new(book_params)
    @book.user = @user
    authorize @book
    if @book.save
      redirect_to user_books_path
      flash[:notice] = 'Success. Your book was added to the Library'
    else
      render "new"
      flash[:notice] = 'Book not created. Please try again'

    end
  end

  def edit
    @user = User.find(params[:user_id])
    @book = Book.find(params[:id])
    authorize @book
  end

  def update
    @book = Book.find(params[:id])
    @book.update(book_params)
    authorize @book
    redirect_to user_book_path
  end

  def destroy
    @book = Book.find(params[:id])
    @book.destroy
    authorize @book
    redirect_to user_books_path
  end

  def tagged
    if params[:tag].present?
      @books = Book.tagged_with(params[:tag])
    else
      @books = Book.all
    end
  end

  private

  def book_params
    params.require(:book).permit(:title,:author,:photo,:comments,:tag_list)
  end
end

book.rb

class Book < ApplicationRecord
  acts_as_taggable_on :tags
  

  belongs_to :user
  has_many :loans


  has_one_attached :photo

  validates :title,presence: true
  validates :author,presence: true

  include PgSearch::Model

  pg_search_scope :search_by_full_name,against: [:title,:author],using: {
      tsearch: {
        prefix: true
      }
    }
end

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...