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>
我认为我的命名约定混乱了,但我找不到正确的方法来做到这一点。任何人有任何建议。此外,过滤器似乎总是空着。我用简单的过滤器测试了很多次,但它从来没有工作。如果有人在我的过滤器中看到错误,请随时指出。谢谢!
所以我的部分应该叫_listing.html.erb? – 2013-03-03 18:54:53
是的,但_listing.html.erb将是单个列表的代表,而不是集合 – 2013-03-03 19:00:04
那么我该如何去显示集合呢?不通过模板? – 2013-03-03 19:00:55