0

我有一个名为 Listing 的模型,我用它来表示转租的列表。我创建了一个名为 Filter 的模型,我用它来根据用户填写的表单过滤子组件。填写表单后,我希望将用户重定向到包含从过滤器返回的所有列表的模板。

这是我的过滤器模型。

class Filter < ActiveRecord::Base
attr_accessible :air_conditioning, :available_rooms, :bathrooms, :furnished, :negotiable, :new, :parking, :maximum_price, :private_bathroom, :show, :term, :total_rooms, :utilities, :washer_dryer
serialize :term

def listings
  @listings ||=find_listings
end

private

def find_listings
  listings=Listing.order(:price)
  listings=Listing.where("listings.price <= ?", maximum_price) if maximum_price.present?
  listings=Listing.where(total_rooms: total_rooms) if total_rooms.present?
  listings=Listing.where(available_rooms: available_rooms) if available_rooms.present?
  listings=Listing.where(bathrooms: bathrooms) if bathrooms.present?
  listings=Listing.where(term: term)
  listings=Listing.where(furnished: furnished)
  listings=Listing.where(negotiable: negotiable)
  listings=Listing.where(utilities: utilities)
  listings=Listing.where(air_conditioning: air_conditioning)
  listings=Listing.where(parking: parking)
  listings=Listing.where(washer_dryer: washer_dryer)
  listings=Listing.where(private_bathroom: private_bathroom)
  listings
  end

end

这是过滤器的显示方法。

<p id="notice"><%= notice %></p>
<%= render (@filter.listings)  %>

很简单。

这是名为 _listing.html.erb 的模板

<div style="padding:5px">
<%= link_to 'New Listing', new_listing_path,{:style=>'', :class => "btn"} %>
<h1>Available Sublets</h1>

<table id="listingTable" class="table table-bordered table-hover">
  <tr>
    <th><%= link_to 'Filter', new_filter_path,{:style=>'', :class => "btn"} %><%= link_to 'Clear Filter', listings_path, {:style=>'', :class => "btn"} %></th>
    <th>Address</th>
    <th><u><%= "Price Per Month" %></u></th>
    <th>Description</th>
  </tr>
<% if @listings !=nil %>
    <% @listings.each do |listing| %>
      <tr onmouseover="this.style.cursor='pointer';"
      onclick="window.location.href = '<%= url_for(:controller => 'listings', :action => 'show', :id=>listing.id) %>' " >
        <td><%= image_tag listing.photo.url(:small) %></td>
        <td><%= listing.address %></td>
        <td>$<%= listing.price %></td>
        <td width="40%"><%= listing.description %></td>
      </tr>
    <% end %>
<% end %>
<% else if @listings==nil %>
    <p> Sorry, No Sublets Fit Your Criteria! </p>
<% end %>
</table>

但是,过滤器永远不会返回任何结果......我已经测试了至少 20 次,这些查询肯定会返回至少 1 个列表。我觉得我有一个命名约定问题,但我以前从未使用过部分。任何帮助都会很棒。

4

1 回答 1

2

这段代码:

listings=Listing.where(term: term)
listings=Listing.where(furnished: furnished)
listings=Listing.where(negotiable: negotiable)
listings=Listing.where(utilities: utilities)
listings=Listing.where(air_conditioning: air_conditioning)
listings=Listing.where(parking: parking)
listings=Listing.where(washer_dryer: washer_dryer)
listings=Listing.where(private_bathroom: private_bathroom)

实际上并没有进一步过滤listings。基本上,它是listings一次又一次地重新分配。

如果要将连续过滤器应用于listings,请执行以下操作:

listings = Listing.where(term: term)
listings = listings.where(furnished: furnished)
listings = listings.where(negotiable: negotiable)
...
于 2013-03-02T22:32:52.387 回答