2013-03-02 258 views
0

我有一個名爲Listing的模型,我用它來表示轉租的列表。我創建了一個名爲Filter的模型,用於基於用戶填寫的表單過濾轉租。填寫完表單後,我希望用戶被重定向到一個模板,該模板包含從過濾器返回的所有列表。Ruby on Rails渲染部分

這是我的過濾器模型。

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> 

然而,過濾器不會返回任何結果的模板......我已經與絕對應該ATLEAST返回查詢測試ATLEAST 20倍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) 
... 
+0

謝謝你,這是真的。但是,即使有這種修復,也沒有任何問題出現。 – 2013-03-02 22:36:42

+0

應該叫做_listing.html.erb還是_listings.html.erb – 2013-03-02 22:38:55