2017-04-24 61 views
0

我的导轨项目中的每个产品都有多个标签,我想通过标签搜索我的产品,并且在搜索时我收到以下错误:使用导轨中的标签搜索产品时找不到标签

的ActiveRecord :: RecordNotFound中的ProductsController#指数

找不到标签

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

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 

模型的Tagging

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

这该是多么I M搜索标签

<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> 

回答

0

在您的产品型号,

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 

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

+0

它摆脱了错误,但没有一个产品显示出来 –

+0

你有没有在你的数据库中的标签(你正在搜索的)? –

+0

是的,他们都是用逗号分隔的(你可以查看这个[link](https://www.youtube.com/watch?v=rzx5MrCa0Pc&t=254s),我是怎么做到的)请帮助 –