【问题标题】:Couldn't find Tag while searching a product with tags in rails在 Rails 中搜索带有标签的产品时找不到标签
【发布时间】:2017-04-24 11:26:12
【问题描述】:

我的 Rails 项目中的每个产品都有多个标签,我想通过标签搜索我的产品,但搜索时出现以下错误:

ActiveRecord::RecordNotFound in ProductsController#index

找不到标签

我使用了一个标签模型来存储标签和一个标签模型来连接标签和产品表(具有多对多关系)

PRODUCTS_CONTROLLER

class ProductsController < ApplicationController
  # before_action :authenticate_user!
  before_action :set_product, only: [:show, :edit, :update, :destroy]

  # GET /products
  # GET /products.json
  def index
     if params[:tag]
    @products = Product.tagged_with(params[:tag])
  else
    @products = Product.all
  end
  end

  # GET /products/1
  # GET /products/1.json
  def show
  end

  # GET /products/new
  def new
    @product = Product.new
  end

  # GET /products/1/edit
  def edit
  end

  # POST /products
  # POST /products.json
  def create
    @product = current_user.products.new(product_params)

    respond_to do |format|
      if @product.save
        format.html { redirect_to @product, notice: 'Product was successfully created.' }
        format.json { render :show, status: :created, location: @product }
      else
        format.html { render :new }
        format.json { render json: @product.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /products/1
  # PATCH/PUT /products/1.json
  def update
    respond_to do |format|
      if @product.update(product_params)
        format.html { redirect_to @product, notice: 'Product was successfully updated.' }
        format.json { render :show, status: :ok, location: @product }
      else
        format.html { render :edit }
        format.json { render json: @product.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /products/1
  # DELETE /products/1.json
  def destroy
    @product.destroy
    respond_to do |format|
      format.html { redirect_to products_url, notice: 'Product was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_product
      @product = Product.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def product_params
      params.require(:product).permit(:filetype, :title, :img_url, :description, :all_tags, :price, :uploaded_by, :tutorial_url)
    end
end

产品型号

class Product < ActiveRecord::Base
    belongs_to :user
    has_many :taggings, dependent: :destroy
    has_many :tags, through: :taggings
    has_many :comments, dependent: :destroy

    has_attached_file :img_url, styles: { large: "800x600>", medium: "320x200>", thumb: "100x80#" }, validate_media_type: false
    validates_attachment_content_type :img_url, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]

    def self.tagged_with(name)
        Tag.find_by!(name: name).products
    end

    def all_tags=(names)
        # names="music, spotify"
        self.tags = names.split(',').map do |name|
            Tag.where(name: name).first_or_create!
        end
    end
    def all_tags
        tags.map(&:name).join(", ")
    end
end

标签模型

class Tag < ActiveRecord::Base
    has_many :taggings, dependent: :destroy
    has_many :products, through: :taggings
end

标签模型

class Tagging < ActiveRecord::Base
  belongs_to :product
  belongs_to :tag
end

这就是我搜索标签的方式

<div id="search-bar">
<%= form_tag products_path, :action=>"index", :method=>"get" do %>
<%= text_field_tag :tag, params[:tag], :name=>"tag", :placeholder=>"   Search Anything", :id=>"s-bar" %>
<%= submit_tag "Search",:name=>"tag", :value=>"search", :style=>"width: 100px; border: none; background: #eee; font-size: 25px; position: relative; top: 5px; display: none;"%>
<%end%>
</div>

【问题讨论】:

    标签: html ruby-on-rails


    【解决方案1】:

    在您的产品模型中,

    def self.tagged_with(name)
     tags = Tag.where('LOWER(name) like ?', "#{name.downcase}")
     products = []
     tags.each do |tag|
      products << tag.products
     end
     return products
    end
    

    这将返回一个产品数组,其标签与您在文本框中搜索的标签相似。

    【讨论】:

    • 它消除了错误,但没有产品出现
    • 您的数据库中有标签(您要搜索的标签)吗?
    • 是的,它们都是用逗号分隔的(你可以检查这个link,我是怎么做到的)请帮忙
    猜你喜欢
    • 2020-12-07
    • 2021-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-16
    • 2012-09-05
    • 1970-01-01
    • 2016-08-07
    相关资源
    最近更新 更多