J'essaie d'ajouter une barre de recherche pour un site Web de base de données que j'ai créé, j'ai trouvé un tutoriel et je "pense" que je l'ai fait correctement.Rails: Barre de recherche ne fonctionne pas
Quand je fais une recherche, comme "Judy Zhang", rien ne montre, même si elle est dans la base de données
mes vendor.rb/préoccupations/modèles/app fichier
class Vendor < ApplicationRecord
has_many :fotos
def self.search(search)
if search
Vendor.where('lower(contact_name) LIKE ?', "'%#{search.downcase}%'")
else
Vendor.all
end
end
end
Je crois que je n'ai pas fait le bon codage. Très nouveau à rubis sur rails. Qu'est-ce que j'ai fait de mal ici?
Codepour index.html.erb/fournisseurs/vues/layouts/app
<body>
<div class = "head">
<h1>Vendors </h1>
<div class = "image1" >
<img src= "http://dx.deucex.com/i/logo.png" >
</div>
</div>
</body>
<table>
<tr>
<%= button_to "New Vendor", new_vendor_path, :method => "get" %>
<%= button_to "Inventory", inventories_path, :method => "get" %>
<%= form_tag vendors_path, :method => 'get' do %>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
<% end %>
</tr>
</table>
<table>
<tr>
<th>Company</th>
<th>Contact Name</th>
<th>Phone</th>
<th>Email</th>
</tr>
<% for vendor in @vendors %>
<tr>
<td><%= vendor.company %></td>
<td><%= vendor.contact_name %></td>
<td><%= vendor.phone %></td>
<td><%= vendor.email %></td>
<body>
<div class = "button1" >
<td><%= button_to "Show", vendor_path(vendor), :method => "get" %></td>
</div>
</body>
<td><%= button_to "Edit", edit_vendor_path(vendor), :method => "get" %></td>
<div class = "button3">
<td><%= button_to 'Delete',
vendor_path(vendor),
method: :delete,
data: { confirm: 'Are you sure?'} %></td>
</div>
</tr>
<% end %>
</table>
Code
pour mon VendorsController.rb/préoccupations/contrôleur/app
class VendorsController < ApplicationController
def index
@vendors = Vendor.search(params[:search])
end
def show
@vendor = Vendor.find(params[:id])
end
def new
@vendor = Vendor.new
end
def create
@vendor = Vendor.new(vendor_params)
if @vendor.save
redirect_to @vendor
else
render 'new'
end
end
def edit
@vendor = Vendor.find(params[:id])
end
def update
@vendor = Vendor.find(params[:id])
if @vendor.update (vendor_params)
redirect_to @vendor
else
render 'edit'
end
end
def destroy
@vendor = Vendor.find(params[:id])
@vendor.destroy
redirect_to vendors_path
end
end
private
def vendor_params
params.require(:vendor).permit(:company, :contact_name, :phone, :email, :moq, :cost_per_item, :payment_method, :terms, :turnover, :returns, :notes)
end
vient d'éditer mon code où j'ai actuellement comme est – bockdavidson
seul problème est quand je fais une recherche , rien ne se montre. Même si la recherche est dans la base de données – bockdavidson