搜索功能不工作 - Ruby 在 rails
search function not working - Ruby on rails
我是 Rails 上 Ruby 的新手,我正在尝试使用简单的形式实现搜索功能以过滤出可用性结果。在我按下搜索按钮后,它仍然停留在同一页面上,而不是 运行 控制器中的搜索功能。允许的是假的。我想知道为什么会发生这种情况。我写的search_params有什么问题吗?
Rails 版本为 6.0.2.1.
views/availabilities/_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>
routes.rb 文件。
get 'search_rides', to: "availabilities#index"
get 'search_rides_result', to: "availabilities#search"
availabilities_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
models/availability.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
与其使用 2 种不同的搜索方法,不如尝试在索引方法中结合使用这两种方法。您的索引方法现在将如下所示:
def index
if params[:search]
@availabilities = Availability.unmatched.search(search_params[:start_city])
else
@availabilities = Availability.unmatched
end
end
将表格 url
更改为 search_rides_path
。这样,搜索结果将在提交表单时呈现在同一视图中。
我是 Rails 上 Ruby 的新手,我正在尝试使用简单的形式实现搜索功能以过滤出可用性结果。在我按下搜索按钮后,它仍然停留在同一页面上,而不是 运行 控制器中的搜索功能。允许的是假的。我想知道为什么会发生这种情况。我写的search_params有什么问题吗? Rails 版本为 6.0.2.1.
views/availabilities/_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>
routes.rb 文件。
get 'search_rides', to: "availabilities#index"
get 'search_rides_result', to: "availabilities#search"
availabilities_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
models/availability.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
与其使用 2 种不同的搜索方法,不如尝试在索引方法中结合使用这两种方法。您的索引方法现在将如下所示:
def index
if params[:search]
@availabilities = Availability.unmatched.search(search_params[:start_city])
else
@availabilities = Availability.unmatched
end
end
将表格 url
更改为 search_rides_path
。这样,搜索结果将在提交表单时呈现在同一视图中。