Ruby on rails 搜索功能不工作-Ruby on rails

Ruby on rails 搜索功能不工作-Ruby on rails,ruby-on-rails,simple-form,Ruby On Rails,Simple Form,我是RubyonRails新手,尝试用简单的表单实现搜索功能,以过滤出可用性结果。在我按下搜索按钮后,它仍然停留在同一页上,并且没有在控制器中运行搜索功能。许可证是假的。我想知道为什么会发生这种情况。我写的搜索参数有什么问题吗? Rails版本是6.0.2.1 视图/可用性/_searchform.html.erb <form> <div id="aDiv"> <%= simple_form_for :search, :url => s

我是RubyonRails新手,尝试用简单的表单实现搜索功能,以过滤出可用性结果。在我按下搜索按钮后,它仍然停留在同一页上,并且没有在控制器中运行搜索功能。许可证是假的。我想知道为什么会发生这种情况。我写的搜索参数有什么问题吗? Rails版本是6.0.2.1

视图/可用性/_searchform.html.erb

<form>
    <div id="aDiv">
        <%= simple_form_for :search, :url => search_rides_result_path, :method => :get do |f| %>
        <div class="form-row">
            <div class="form-group col-md-2">
                <%= f.input :start_city, label: 'Start City', class: "form-control", error: 'Start address is mandatory, please specify one' %>
            </div>
            <div class="form-group col-md-4">
                <%= f.input :start_street_address, label: 'Start Street Address', class: "form-control" %>
            </div>
        </div>
        <div class="form-row">
            <div class="form-group col-md-2">
                <%= f.input :end_city, label: 'Destination City', class: "form-control" %>
            </div>
            <div class="form-group col-md-4">
                <%= f.input :end_street_address, label: 'Destionation Street Address', class: "form-control" %>
            </div>
        </div>
        <div class="form-row">
            <div class="form-group col-md-4">
                <%= f.input :trip_time, as: :datetime, inline_label: 'Yes, remember me', class: "form-control" %>
            </div>
            <div class="form-group col-md-2">
                <%= f.input :lowest_acceptable_price, label: 'Expected Price', class: "form-control" %>
            </div>
        </div>
            <%= f.submit "Search", class: "btn btn-primary" %>
        <% end %>
    </div>
</form>
可用性_controller.rb

  def index
    @availabilities = Availability.unmatched
  end

  def search
    @availabilities = Availability.unmatched.search(search_params[:start_city])
  end

  def search_params
    params.require(:search).permit(:start_city, :start_street_address, :end_city, :end_street_address, :trip_time, :lowest_acceptable_price)
  end
型号/可用性.rb

class Availability < ApplicationRecord
    has_many :users, :through => :posts
    has_one :request
    scope :unmatched, ->{ where(matched_request_id: -1) }

    def self.search (params)
        puts "start searching"
        results = city_matches(start_city, end_city, params[:start_city], params[:end_city]) 
        return nil unless results
        results
    end
类可用性:帖子
有一个:请求
作用域:不匹配,->{where(匹配的请求\u id:-1)}
def self.search(参数)
放置“开始搜索”
结果=城市匹配(开始城市、结束城市、参数[:开始城市]、参数[:结束城市])
除非有结果,否则返回零
结果
结束

不要使用两种不同的搜索方法,而是尝试在索引方法中同时使用这两种方法。您的索引方法现在将如下所示:

def index
  if params[:search]
    @availabilities = Availability.unmatched.search(search_params[:start_city])
  else
    @availabilities = Availability.unmatched
  end
end

将表单
url
更改为
search\u rides\u path
。这样,在提交表单时,搜索结果将呈现在相同的视图中。

您看到get请求了吗?日志中的参数是什么样的?@DanHilton如果我在表单中键入一些内容,参数如下:参数:{“搜索”=>{“开始城市”=>“沃尔瑟姆”,“开始街道地址”=>“某条街”,“结束城市”=>“沃尔瑟姆”,“结束街道地址”=>“其他街道”,“出行时间(1i)”=>“2020”,“出行时间(2i)”=>“3”,“出行时间(3i)”=>“25”,“出行时间(4i)”=>“18”,“trip\u time(5i)”=>“05”,“最低可接受价格”=>“5”},“提交”=>“搜索”}我认为get请求可以工作,因为这些参数被附加到url。但是,搜索函数中的任何内容都不会执行。我明白了。我想知道这是否与您的search.html.erb模板有关(因为表单操作正在影响您的搜索操作)。通常使用搜索时,我看到搜索表单路径提交到索引操作。通过这种方式,控制器可以确定是否进行任何必要的过滤,但视图是最重要的same@DanHilton谢谢让我看看它是否有效!
def index
  if params[:search]
    @availabilities = Availability.unmatched.search(search_params[:start_city])
  else
    @availabilities = Availability.unmatched
  end
end