2013-03-03 59 views
0

我有一個列表模型,我用它來表示一個轉租公寓(apartment)的列表,並且我創建了一個過濾模式作爲用戶根據他的喜好過濾商品的方式。在我的Filter類中,它有一個基於表單提交來查詢列表的方法列表。如何在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=listings.where("listings.price <= ?", maximum_price) if maximum_price.present? 
    listings=listings.where(total_rooms: total_rooms) if total_rooms.present? 
    listings=listings.where(available_rooms: available_rooms) if available_rooms.present? 
    listings=listings.where(bathrooms: bathrooms) if bathrooms.present? 
    listings=listings.where(term: term) 
    listings=listings.where(furnished: furnished) 
    listings=listings.where(negotiable: negotiable) 
    listings=listings.where(utilities: utilities) 
    listings=listings.where(air_conditioning: air_conditioning) 
    listings=listings.where(parking: parking) 
    listings=listings.where(washer_dryer: washer_dryer) 
    listings=listings.where(private_bathroom: private_bathroom) 
    listings 
    end 

end 

爲了顯示這些,我創建了一個用於過濾器的顯示方法,我想渲染部分模板。這是我現在有,但它不會使我創建了名爲_listings.html.erb的模板/列表

<p id="notice"><%= notice %></p> 

<%= @filter.listings.size %> 

<%= render @filter.listings %> 

這裏是模板

<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> 

我認爲我的命名約定混亂了,但我找不到正確的方法來做到這一點。任何人有任何建議。此外,過濾器似乎總是空着。我用簡單的過濾器測試了很多次,但它從來沒有工作。如果有人在我的過濾器中看到錯誤,請隨時指出。謝謝!

回答

1

如果您調用render並將其傳遞給一個數組或關係,它實際上會調用部分的單數版本。當你調用

<%= render @filter.listings %> 

什麼它最後做的是等效於以下方法:

<%= render :partial => 'listings/listing', :collection => @filter.listings %> 

這相當於

<% @filter.listings.each do |listing| %> 
    <%= render listing %> 
<% end %> 

此外,還有周圍的錯誤或部分的` <%end%> <%否則如果...%>,應該是以下內容

<% 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 %> 
<% else %> (since you already checked if it was nil, you it is implied at this point the value is nil 
    <p> Sorry, No Sublets Fit Your Criteria! </p> 
<% end %> 

我建議寫它下面的方式,因爲它是更紅寶石去年秋季

<% if @listings.blank? %> 
    <p> Sorry, No Sublets Fit Your Criteria! </p> 
<% else %> 
    <% @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 %> 

我最終會處理這種情況的方法如下:

你的過濾器顯示視圖

<p id="notice"><%= notice %></p> 

<%= @filter.listings.size %> 
<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> 
    <%= render(@filter.listings) || "<p> Sorry, No Sublets Fit Your Criteria! </p>".html_safe %> 
</table> 

你 '上市/ _listing.html.erb'

<tr onmouseover="this.style.cursor='pointer';" 
    onclick="window.location.href = '<%= url_for(listing) %>' " > 
    <td><%= image_tag listing.photo.url(:small) %></td> 
    <td><%= listing.address %></td> 
    <td>$<%= listing.price %></td> 
    <td width="40%"><%= listing.description %></td> 
</tr> 
+0

所以我的部分應該叫_listing.html.erb? – 2013-03-03 18:54:53

+0

是的,但_listing.html.erb將是單個列表的代表,而不是集合 – 2013-03-03 19:00:04

+0

那麼我該如何去顯示集合呢?不通過模板? – 2013-03-03 19:00:55