0

我有一个列表模型,我用它来表示一个转租(公寓)的列表,我创建了一个过滤器模型作为用户根据自己的喜好过滤列表的一种方式。在我的 Filter 类中,它有一个方法 Listings 来根据表单提交查询列表。

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

为了显示这些,我为要呈现部分模板的过滤器创建了一个 show 方法。这是我目前拥有的,但它不会呈现我在 /listings 中创建的名为 _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>

我认为我的命名约定搞砸了,但我找不到正确的方法来做到这一点。任何人都有任何建议。此外,过滤器似乎总是空着。我已经用简单的过滤器对其进行了多次测试,但它从来没有用过。如果有人在我的过滤器中看到错误,请随时指出。谢谢!

4

1 回答 1

1

如果你调用 render 并传递给它一个数组或关系,它实际上会调用部分的单数版本。你打电话时

<%= render @filter.listings %>

它最终做的等同于以下内容:

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

这相当于

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

另外,`<% end %> <% else if ... %>周围有错误或部分错误,应该是下面这样

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

我建议用以下方式编写它,因为它更像 Ruby-esque

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

你的'listings/_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>
于 2013-03-03T18:54:09.980 回答