Ruby on rails Rails搜索多个查询不起作用
我知道我的代码很笨重,但我正在尝试为我的Ruby on rails Rails搜索多个查询不起作用,ruby-on-rails,forms,Ruby On Rails,Forms,我知道我的代码很笨重,但我正在尝试为我的GUEST数据库中的不同列创建一个包含多个文本框的表单。该表单不起作用,所有my guest条目仍在列表中(列表不会因搜索而缩小)。如果有人能给我指出正确的方向,那就太棒了。另外,guest.rb代码如何更简洁 控制器: def index @guests = Guest.all if params[:search_first_name] @guests = Guest.search_first_name(params[:sea
GUEST
数据库中的不同列创建一个包含多个文本框的表单。该表单不起作用,所有my guest条目仍在列表中(列表不会因搜索而缩小)。如果有人能给我指出正确的方向,那就太棒了。另外,guest.rb代码如何更简洁
控制器:
def index
@guests = Guest.all
if params[:search_first_name]
@guests = Guest.search_first_name(params[:search_first_name])
end
if params[:search_last_name]
@guests = Guest.search_last_name(params[:search_last_name])
end
if params[:search_email]
@guests = Guest.search_email(params[:search_email])
end
if params[:search_phone]
@guests = Guest.search_phone(params[:search_phone])
end
end
guest.rb
def self.search_first_name(query)
# where(:email, query) -> This would return an exact match of the query
where("first_name like ?", "%#{query}%")
end
def self.search_last_name(query)
# where(:email, query) -> This would return an exact match of the query
where("last_name like ?", "%#{query}%")
end
def self.search_email(query)
# where(:email, query) -> This would return an exact match of the query
where("email like ?", "%#{query}%")
end
def self.search_phone(query)
# where(:email, query) -> This would return an exact match of the query
where("phone like ?", "%#{query}%")
end
index.html:
<%= form_tag(guests_path, :method => "get", class: "navbar-form", id: "search-form") do %>
<div class="input-append col-sm-10">
<div class="row">
<div class="col-sm-4">
<p><%= text_field_tag :search_first_name, params[:search_first_name], placeholder: "First Name", style: "width:100%;" %></p>
<p><%= text_field_tag :search_last_name, params[:search_last_name], placeholder: "Last Name", style: "width:100%;" %></p>
</div>
<div class="col-sm-4">
<p><%= text_field_tag :search_email, params[:search_email], placeholder: "Email Address", style: "width:100%;" %></p>
<p><%= text_field_tag :search_phone, params[:search_phone], placeholder: "Phone Number", style: "width:100%;" %></p>
</div>
<div class="col-sm-4">
</div>
<%= submit_tag 'Search Guests' %>
</div>
</div>
<% end %>
<table class="table table-hover col-sm-12">
<thead>
<tr>
<th>Guest ID</th>
<th>Guest Name</th>
<th>Email</th>
<th>Phone</th>
<th></th>
</tr>
</thead>
<tbody>
<% @guests.each do |guest| %>
<tr>
<td>G-00<%= guest.id %></td>
<td><%= guest.first_name %> <%= guest.last_name %></td>
<td><%= guest.email %></td>
<td><%= guest.phone %></td>
<td style="text-align:right;"><%= link_to 'View Guest Profile', guest, :class => "btn btn-sm btn-success" %> <%= link_to 'Edit', edit_guest_path(guest), :class => "btn btn-sm btn-default" %> <%= link_to 'Destroy', guest, method: :delete, data: { confirm: 'Are you sure?' }, :class => "btn btn-sm btn-danger" %></td>
</tr>
<% end %>
</tbody>
</table>
“获取”,类:“导航栏表单”,id:“搜索表单”)do%>
客人ID
客人姓名
电子邮件
电话
G-00
“btn btn sm btn成功”%%>“btn btn sm btn默认”%%>“btn btn sm btn危险”%%>
您应该放置“^#{query}”
使其作为正则表达式工作。这样,您的Guest.rb文件将如下所示
Guest.rb
def self.search_first_name(query)
# where(:email, query) -> This would return an exact match of the query
where("first_name like ?", "^#{query}")
end
def self.search_last_name(query)
# where(:email, query) -> This would return an exact match of the query
where("last_name like ?", "^#{query}")
end
def self.search_email(query)
# where(:email, query) -> This would return an exact match of the query
where("email like ?", "")
end
def self.search_phone(query)
# where(:email, query) -> This would return an exact match of the query
where("phone like ?", "%#{query}%")
end
您应该放置“^#{query}”
以使其作为正则表达式工作。这样,您的Guest.rb文件将如下所示
Guest.rb
def self.search_first_name(query)
# where(:email, query) -> This would return an exact match of the query
where("first_name like ?", "^#{query}")
end
def self.search_last_name(query)
# where(:email, query) -> This would return an exact match of the query
where("last_name like ?", "^#{query}")
end
def self.search_email(query)
# where(:email, query) -> This would return an exact match of the query
where("email like ?", "")
end
def self.search_phone(query)
# where(:email, query) -> This would return an exact match of the query
where("phone like ?", "%#{query}%")
end
没有深入到代码重构。尝试添加
params[:field_name]。在所有if
语句中显示?
。无需深入代码重构。尝试添加params[:field_name]。将?
添加到所有if
语句中。出现问题的原因是,没有值的param将等于”
,在Ruby中仍然是true
,因此基本上,如果搜索电话
为空,那么您将看到所有结果,因为if params[:search_phone]
将为true,因此将运行该块,并且运行的最终代码将等效于:
@guests = Guest.where( "phone like '%%'" )
这只是两个通配符,因此将返回所有结果。其他人已经提到将该条件更改为使用.present?
,因此如果该条件为空,则不会运行它
你也问过重构代码,我会这样写:
控制器
模型
这个问题是因为一个没有值的param将等于
”
,在Ruby中仍然是true
,所以基本上如果你的search\u phone
是空的,那么你将看到所有的结果,因为if params[:search\u phone]
将为true,因此将运行该块,并且运行的最终代码将等效于:
@guests = Guest.where( "phone like '%%'" )
这只是两个通配符,因此将返回所有结果。其他人已经提到将该条件更改为使用.present?
,因此如果该条件为空,则不会运行它
你也问过重构代码,我会这样写:
控制器
模型
搜索后显示页面时,是否有任何搜索字段已填写您提交的内容?是,搜索后字段仍将填写,URL也会反映这一点。搜索后显示页面时,是否有任何搜索字段已填写您提交的内容?是,搜索后字段仍将填写g和URL也反映了这一点抱歉,做了一个小的更正,我做了
=
而不是像这样的应该也去掉搜索
这个词吗?理想情况下,你一开始就不会有这个。重构很好。做得很好。抱歉,做了一个小的更正,我做了=
而不是类似的p单词search\uu
?理想情况下,你一开始就不会有这个词。重构不错。干得好。