我想爲我創建的數據庫網站添加一個搜索欄,我找到了一個教程,我「認爲」我做的正確。Rails:搜索欄不工作
當我做了搜索,如「朱迪張」,什麼也不顯示,即使是在數據庫
我vendor.rb /顧慮/模型/ app文件
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
我相信我沒有做編碼權。對軌道上的紅寶石非常新穎。我在這裏做錯了什麼?
爲index.html.erb /供應商/視圖/佈局/應用程序代碼
<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>
我VendorsController.rb /顧慮/控制器/應用
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
剛編輯我的代碼,我現在把它當作是 – bockdavidson
唯一的問題是,當我做搜索,什麼都沒有出現。即使搜索是在數據庫中 – bockdavidson