我正在尝试为我创建的数据库网站添加搜索栏,我找到了一个教程,我“想”我做错了。
当我进行搜索时,例如“Judy Zhang”,即使它在数据库中也没有显示任何内容
我的vendor.rb / concerns / models / app file
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 / vendors / views / 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>
我的VendorsController.rb / concerns / controller / 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
答案 0 :(得分:0)
尝试将Vendor
中的代码更改为
class Vendor < ApplicationRecord
has_many :fotos
def self.search(search)
if search
Vendor.where('lower(name) LIKE ?', "'%#{search.downcase}%'")
else
Vendor.all
end
end
end
我的搜索栏没有显示
您应该更改以下内容
<% form_tag vendors_path, :method => 'get' do %>
到
<%= form_tag vendors_path, :method => 'get' do %>
# Notice that <%= %> evaluates and prints the output