Rails 使用了错误的操作,搜索参数附加到当前 URL

Rails using wrong action, search parameters appended to current URL

我在导航栏中有一个搜索框,它应该在指定页面上显示搜索结果 /listings/search。但是由于某种原因,搜索参数附加到当前 URL,而不是我在代码中指定的

这意味着我经常得不到返回的搜索结果,只有当前页面。

表单代码:

<%= simple_form_for :search, url: search_listings_path, method: :get do |f| %>
  <div class='input-group'>
    <%= f.input :address, label_html: { class: 'sr-only' }, label: 'Address', id: 'search_address', placeholder: 'Search', input_html: { value: session[:address] }, required: false %>

    <span class='input-group-btn'>
      <button class='btn btn-primary' type='submit' aria-hidden='true' aria-label='Search'><i class='fa fa-search'></i></button>
    </span>
  </div>

  <%= render '/listings/address_autocomplete' %>
<% end %>

此代码生成以下内容 HTML:

<form novalidate="novalidate" class="simple_form search" action="/listings/search" accept-charset="UTF-8" method="get"><input name="utf8" type="hidden" value="&#x2713;" />
  <div class='input-group'>
    <div class="form-group string optional search_address"><label class="string optional sr-only control-label" for="search_address">Address</label><input value="London, United Kingdom" class="string optional form-control" placeholder="Search" type="text" name="search[address]" id="search_address" /></div>

    <span class='input-group-btn'>
      <button class='btn btn-primary' type='submit' aria-hidden='true' aria-label='Search'><i class='fa fa-search'></i></button>
    </span>
  </div>

<script type='text/javascript'>
  function initialize() {
    var input = document.getElementById('search_address');

    var autocomplete = new google.maps.places.Autocomplete(input);
  }

  google.maps.event.addDomListener(window, 'load', initialize);
</script>

</form>

动作/listings/search正确。

但是 Rails 将搜索参数附加到当前 URL。示例输出:

Started GET "/listings/13?utf8=%E2%9C%93&search%5Baddress%5D=London%2C+United+Kingdom" for ::1 at 2015-08-29 19:19:17 +0100
Processing by ListingsController#show as HTML
Parameters: {"utf8"=>"✓", "search"=>{"address"=>"London, United Kingdom"}, "id"=>"13"}

Rails 使用 show 而不是 search

预计URL:

http://localhost:3000/listings/search?utf8=%E2%9C%93&search%5Baddress%5D=London%2C+United+Kingdom

实际 URL:

http://localhost:3000/listings/13?utf8=%E2%9C%93&search%5Baddress%5D=London%2C+United+Kingdom

routes.rb的相关部分:

resources :listings do
  collection do
    get 'search'
  end

  resources :orders, only: [:new, :create]
end

rake routes的相关部分:

search_listings GET    /listings/search(.:format)                 listings#search

对可能出现的问题有什么想法吗?

listings#search 的控制器代码:

def search
  # persist address search parameter 
  session[:address] = params[:search][:address].presence || ""

  # define initial set of listings with no filters applied
  @listings = Listing.all.order('created_at DESC')

  # restrict to 100-km radius and sort by distance if address entered
  if params[:search][:address].present?
    @listings = Listing.all.near(params[:search][:address], 100, units: :km, order: 'distance')
  end

  # filter by price
  if params[:search][:price_lower].present? || params[:search][:price_upper].present?
    @listings = @listings.where('price >= ? AND price <= ?', params[:search][:price_lower], params[:search][:price_upper])
  end

  # filter by size
  if params[:search][:size_lower].present? || params[:search][:size_upper].present?
    @listings = @listings.where('size >= ? AND size <= ?', params[:search][:size_lower], params[:search][:size_upper])
  end

  # filter by space type
  if params[:search][:space_ids].nil? || params[:search][:space_ids].size <= 1
    @listings = @listings.group(:id)
  else
    @listings = @listings.joins(:spaces).where('space_id IN (?)', params[:search][:space_ids].reject!(&:blank?))
  end

  # filter by amenities
  if params[:search][:amenity_ids].nil? || params[:search][:amenity_ids].size <= 1
    @listings = @listings.group(:id)
  else
    @listings = @listings.joins(:amenities).where(:amenities => { :id => params[:search][:amenity_ids].reject!(&:blank?) }).group(:id).having('count(*) = ?', params[:search][:amenity_ids].size)
  end

  @hash = Gmaps4rails.build_markers(@listings) do |listing, marker|
    marker.lat listing.latitude
    marker.lng listing.longitude
    marker.infowindow listing.address
    marker.json({ :id => listing.id })
    marker.infowindow render_to_string(:partial => '/listings/map_infowindow', :locals => { :listing => listing })
  end
end

(一旦一切正常,我将进行重构。)

我认为这与您的路线和控制器无关——一切看起来都不错。如果你直接调用你期望的 URL,结果是可以的,对吧?

你能展示更多你生成的 HTML 吗?页面中还有其他形式吗?一些 javascript 绑定其他东西?这个 id 13 是从哪里来的(它可能有帮助)?

我想通了这个问题。问题是由围绕我的搜索框部分的 Bootstrap 表单 class 代码引起的:

<form class="navbar-form navbar-left" role="search">
  ...
</form>

删除表单 class 代码修复了问题,搜索框按预期工作。

但是,这给我留下了一个对于导航栏来说太大的搜索框,现在我需要找到合适的 class 以使其适合并正确定位。

谢谢大家。