简体   繁体   English

Ruby on Rails渲染部分

[英]Ruby on Rails Render Partial

I have a model called Listing that I use to represent a listing for a sublet. 我有一个名为Listing的模型,用于表示转租的列表。 I created a model called Filter that I use to filter the sublets based on a form that the user fills out. 我创建了一个名为Filter的模型,用于根据用户填写的表单过滤转租。 Upon filling out the form, I want the user to be redirected to a template that has all of the listings that were returned from the filter. 在填写表单后,我希望将用户重定向到包含从过滤器返回的所有列表的模板。

Here is my Filter model. 这是我的过滤器模型。

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

Here is the show method for filter. 这是过滤器的show方法。

<p id="notice"><%= notice %></p>
<%= render (@filter.listings)  %>

Pretty simple. 很简单。

And here is the template called _listing.html.erb 这是名为_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>

However, the filter never returns any results... I have tested atleast 20 times with queries that should definitely return atleast 1 listing. 但是,过滤器永远不会返回任何结果...我已经测试了至少20次查询应该肯定返回至少1个列表。 I feel like I have a naming convention problem but I have never used partials before. 我觉得我有一个命名约定问题,但我以前从未使用过partial。 any help would be great. 任何帮助都会很棒。

This code: 这段代码:

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)

Is not actually filtering listings down further. 实际上并没有进一步过滤listings Basically, it's reassigning listings again and again and again. 基本上,它一次又一次地重新分配listings

If you want to apply successive filters to listings , do this: 如果要将连续过滤器应用于listings ,请执行以下操作:

listings = Listing.where(term: term)
listings = listings.where(furnished: furnished)
listings = listings.where(negotiable: negotiable)
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM