Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails搜索多个查询不起作用_Ruby On Rails_Forms - Fatal编程技术网

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
?理想情况下,你一开始就不会有这个词。重构不错。干得好。