控制器
class GearsController < ApplicationController
before_action :set_gear, only: [:show, :edit, :update, :destroy]
def primary
@gears = Gear.search do
with(:Lab, 'Primary')
order_by(:RU, :desc)
end
end
模型
class Gear < ActiveRecord::Base
validates_uniqueness_of :Nic1mac, :Nic2mac, :Nic3mac, :IBmac, :SN, :allow_blank => true
attr_readonly :Devtype, :Nic1mac, :Nic2mac, :Nic3mac, :IBmac, :SN
scope :Mini, -> { where(lab: 'Mini') }
scope :Primary, -> { where(lab: 'Primary') }
scope :Support, -> { where(lab: 'Support') }
scope :M5, -> { where(lab: 'M5') }
scope :P5, -> { where(lab: 'P5') }
searchable do
integer :RU
text :Devname
text :Devtype
string :Lab
text :Swportib
text :Swport1
text :Swport2
text :Swport3
text :IBip
text :Nic1
text :Nic2
text :Nic3
text :Nic1mac
text :Nic2mac
text :Nic3mac
text :IBmac
text :Psu1
text :Psu2
text :SN
end
end
查看
<% provide(:title, "SP4 Primary") %>
<p id="notice"><%= notice %></p>
<h1>SP4 Primary</h1>
<%= form_tag gears_path, :method => :get do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
<head>
<style>
table, th, td { border: 1px solid black;}
th, td { padding: 15px;}
</style>
</head>
<table>
<thead>
<tr>
<th>RU</th>
<th>Dev Name</th>
<th>Dev Type</th>
<th>Lab</th>
<th>SW PortIB</th>
<th>SW Port1</th>
<th>SW Port2</th>
<th>SW Port3</th>
<th>IB IP</th>
<th>NIC1</th>
<th>NIC2</th>
<th>NIC3</th>
<th>NIC1 Mac</th>
<th>NIC2 Mac</th>
<th>NIC3 Mac</th>
<th>IB Mac</th>
<th>PSU1</th>
<th>PSU2</th>
<th>SN</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @gears.each do |gear| %>
<tr>
<td><%= gear.RU %></td>
<td><%= gear.Devname %></td>
<td><%= gear.Devtype %></td>
<td><%= gear.Lab %></td>
<td><%= gear.Swportib %></td>
<td><%= gear.Swport1 %></td>
<td><%= gear.Swport2 %></td>
<td><%= gear.Swport3 %></td>
<td><%= gear.IBip %></td>
<td><%= gear.Nic1 %></td>
<td><%= gear.Nic2 %></td>
<td><%= gear.Nic3 %></td>
<td><%= gear.Nic1mac %></td>
<td><%= gear.Nic2mac %></td>
<td><%= gear.Nic3mac %></td>
<td><%= gear.IBmac %></td>
<td><%= gear.Psu1 %></td>
<td><%= gear.Psu2 %></td>
<td><%= gear.SN %></td>
<td><%= link_to 'Show', gear %></td>
<td><%= link_to 'Edit', edit_gear_path(gear) %></td>
<td><%= link_to 'Destroy', gear, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
以上是我的控制器和型号代码。我正在尝试设置搜索,因此它只搜索指定范围内的记录。目前它将返回所有结果,并不注意范围。我相信我已接近使它工作,但只是没有正确的语法。然后我可能是错的,因此任何帮助建议或不同的方式都是受欢迎的。
答案 0 :(得分:0)
您可以在搜索方法中添加这样的代码
with(:lab, 'Support')
并在您的模型中添加此
string :lab
答案 1 :(得分:0)
解决方案是在我的gears_controller.rb文件中添加without
。
<强> gears_controller.rb 强>
def rack1
@gears = Gear.where("Lab like ? OR Lab like ? OR Lab like ?", "%Primary%", "%Mini%", "%Support%")
@search = Gear.search do
fulltext params[:search]
without(:Lab, "P5")
without(:Lab, "M5")
order_by(:RU, :desc)
end
@gears = @search.results
end
添加不带字段允许我仅使用我的@gears
变量中指定的特定Lab值搜索记录。