How to make a search field with form_tag in rails?

1

Hello! Good afternoon. I'm an information systems student and I'm doing a small project where I need to put a form to fetch the Product template objects that exist in the database.

Product template:

class Produto < ApplicationRecord
  belongs_to :fornecedor

  def self.search(search)
    if search
      find(:all, :conditions => ['name LIKE ?', "%#{search}%"])
    else
      find(:all)
    end
  end

end

Products controller:

class ProdutosController < ApplicationController
  attr_accessor :nome
  before_action :set_produto, only: [:show, :edit, :update, :destroy]

  # GET /produtos
  # GET /produtos.json
  def index
    @produtos = Produto.all
  end

  def show
  end

  def getbusca
    @produts = Produto.search(params[:search])
  end

  def new
    @produto = Produto.new
  end

  def edit
  end

  def create
    @produto = Produto.new(produto_params)
    respond_to do |format|
      if @produto.save
        format.html { redirect_to @produto, notice: 'Produto was successfully created.' }
        format.json { render :show, status: :created, location: @produto }
      else
        format.html { render :new }
        format.json { render json: @produto.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    respond_to do |format|
      if @produto.update(produto_params)
        format.html { redirect_to @produto, notice: 'Produto was successfully updated.' }
        format.json { render :show, status: :ok, location: @produto }
      else
        format.html { render :edit }
        format.json { render json: @produto.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @produto.destroy
    respond_to do |format|
      format.html { redirect_to produtos_url, notice: 'Produto was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def produto_params
      params.require(:produto).permit(:nome, :preco, :fornecedor_id)
    end
end

The home.html.erb view of the controlle Home:

<%= form_tag produtos_path, :action => 'getbusca', :method => 'get' do %>
      <p>
        <%= text_field_tag :search, params[:search] %>
        <%= submit_tag "Search", :name => nil %>
      </p>
  <% end %>

routes:

Rails.application.routes.draw do

  root 'login#new'
  get '/home/inicio', to: 'home#index'
  get '/produtos', to: 'produtos#getbusca'

  scope '/login' do
    post '/', to: 'login#criarusuario'
    get '/acesso', to:'login#new'
    post '/acessorecebendo', to:'login#create'
    get '/sair', to:'login#destroy'
  end

  resources :login
  resources :home
  resources :produtos
  resources :fornecedors
end

The error:

  

Could not find all Products with 'id': (all, {: conditions = > ["name LIKE   ","% rice% "]) (found 0 results, but was looking for 2)

on line:

  def self.search(search)
    if search
      **find(:all, :conditions => ['name LIKE ?', "%#{search}%"])**
    else
      find(:all)
    end
    
asked by anonymous 07.06.2017 / 20:12

1 answer

1

Hello, in your form you are putting produtos_path which refers to the ProductsController # index of your application.

For you to getbusca action you need to create this route and add it to your form.

Or one approach that I think would be best to place your search in the same Index, replacing

  def index
    @produtos = Produto.all
  end

by

  def index
    @produtos = Produto.search(params[:search])
  end

I think I'd work it out already.

EDIT:

This error that you presented is generated because the find method since Rails 4 has been deprecated, so you should use where in place, following example:

 def self.search(search)
    if search
      where(['name LIKE ?', "%#{search}%"])
    else
      all
    end
 end
    
08.06.2017 / 15:23