我有一个简单的簿记 rails 应用程序,它实现了 Act_as_Taggable_on 并在我的索引上显示所有创建的标签,但是当我选择一个时,它会返回所有索引,而不仅仅是与该标签关联的元素;在这个问题上需要一些帮助,拜托。我的代码是:
路线.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
书籍控制器
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
书本.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