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

时间:2015-08-29 20:52:02

标签: ruby-on-rails rails-routing

我在导航栏中有一个搜索框,可以在指定的页面/列表/搜索中显示搜索结果。但由于某种原因,搜索参数被附加到当前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

预期网址:

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

实际网址:

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

(一旦一切正常,我会重构。)

2 个答案:

答案 0 :(得分:0)

我不认为它与你的路线和控制器有关 - 一切看起来都正确。如果您直接调用预期的URL,结果就可以了,对吧?

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

答案 1 :(得分:0)

我想出了这个问题。问题是由我的搜索框partial:

周围的Bootstrap表单类代码引起的
<form class="navbar-form navbar-left" role="search">
  ...
</form>

删除表单类代码可以解决问题,搜索框可以按预期工作。

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

谢谢大家。