找不到带有' id' =搜索

时间:2017-04-17 00:06:30

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4

我刚从RoR开始,我遇到了一个问题。

早些时候,我问过听到,没人能解决。我想做一个简单的搜索表单,在那里我会搜索产品名称(标题)。

以下是问题:"无法找到带有' id' =搜索"

的产品

Picture with the error

这是我的控制器:

class ProductsController < ApplicationController
  before_action :set_product, only: [:show, :edit, :update, :destroy]
  helper_method :get_cost

  # GET /products
  def index
    @products = Product.all
    @products = Product.search(params[:find])
  end

  # GET /products/1
  def show
  end

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

  # GET /products/1/edit
  def edit
  end

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

    if @product.save
      redirect_to @product, notice: 'Product was successfully created.'
    else
      render :new
    end
  end

  # PATCH/PUT /products/1
  def update
    if @product.update(product_params)
      redirect_to @product, notice: 'Product was successfully updated.'
    else
      render :edit
    end
  end

  # DELETE /products/1
  def destroy
    @product.destroy
    redirect_to products_url, notice: 'Product was successfully destroyed.'
  end

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

    # Only allow a trusted parameter "white list" through.
    def product_params
      params.require(:product).permit(:title, :description, :category,   :price, :quantity, {ingredient_ids: []})
    end
end

这是我的模特:

class Product < ActiveRecord::Base
  has_and_belongs_to_many :ingredients
  has_and_belongs_to_many :orders

  enum category: ["Culinária Italiana", "Culinária Oriental", "Culinária Árabe", "Culinária Brasileira"]

  TITLE_MIN_LENGTH = 3
  DESCRIPTION_MIN_LENGTH = 2

  validates :title, presence: true, :length => {:minimum => TITLE_MIN_LENGTH}, uniqueness: true
  validates :description, presence: true, :length => {:minimum => DESCRIPTION_MIN_LENGTH}

  def self.search(search)
    if search
        where(["title LIKE ?", "%#{search}%"])
    else
        all
    end
  end


end

这是我的观点:

<%= form_tag(products_search_path, :method => :get) do%>
    <%= text_field_tag :find, nil, placeholder:"Insira o que está procurando"%>
    <%= submit_tag "Pesquisar", :name => nil %> 
<% end %>

刚刚结束,这是我的路线:

Rails.application.routes.draw do
  get 'sessions/new'

  get 'users/new'

  get 'orders/show'
  get 'orders/new'

  resources :ingredients

  resources :products do
    get 'search'
  end

  resources :orders

  root "home#index"
  get    '/login',   to: 'sessions#new'
  post   '/login',   to: 'sessions#create'
  delete '/logout',  to: 'sessions#destroy'
  get  '/signup',  to: 'users#new'
  post '/signup',  to: 'users#create'

  get 'home/about'
  get 'home/contact'

  get "home" => "home#index"

end

感谢您的关注,Miguel。

2 个答案:

答案 0 :(得分:1)

您的routes.rb中有get 'search',但您的控制器中没有def search。所以它认为你正试图访问节目页面:

GET products/[id]其中[id]search(显然无效,因为所有ID都是数字)。

如果要保留search路由,则应将其设置为: get 'search', on: :collection。并使用搜索逻辑将def search添加到控制器。

编辑:

只需添加一些提示:

  • def index方法中,此行@products = Product.all是不必要的,因为您在以下行中覆盖它;
  • 建议您将search与查询字符串一起使用,而不是为index创建不同的路由。所以你应该:

    • get 'search';
    • 中删除routes.rb
    • 将您的搜索表单路径更改为products_path,而不是products_search_path;

答案 1 :(得分:0)

search是一种收集方法,因此路线需要......

resources :products do
  collection do
    get 'search'
  end
end