0

I have a model called Listing that I use to represent a listing for a sublet. I created a model called Filter that I use to filter the sublets based on a form that the user fills out. 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.

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

Pretty simple.

And here is the template called _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. I feel like I have a naming convention problem but I have never used partials before. any help would be great.

1 Answer 1

2

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. Basically, it's reassigning listings again and again and again.

If you want to apply successive filters to listings, do this:

listings = Listing.where(term: term)
listings = listings.where(furnished: furnished)
listings = listings.where(negotiable: negotiable)
...
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, that is true. However, even with that fix, nothing comes up.
should it be called _listing.html.erb or _listings.html.erb

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.