Ruby on Rails渲染部分

时间:2013-03-02 22:30:30

标签: ruby-on-rails templates partial rails-activerecord

我有一个名为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

以下是过滤器的show方法。

<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个列表。我觉得我有一个命名约定问题,但我以前从未使用过partial。任何帮助都会很棒。

1 个答案:

答案 0 :(得分: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)
...