views:

29

answers:

1

I get the exact result I want if I change...

def index
 @listings = Listing.all
end

to...

def index
 @listings = Listing.where("general_use == 'Industrial'")
end

... in listings_controller.rb

The Listings index view shows a list of all Listings where the general_use field contains the word Industrial. However, I can no longer use the index view to show all listings if I do this.

I want to add a link at the top of my listings index view that narrows the listings down from "all" to just the "industrial" listings.

I don't know what code should go in any, all or none of the following places:

  • controllers\listings_controller.rb
  • helpers\listings_helpers.rb
  • models\listing.rb
  • views\listings\index.html.erb
  • config\routes.rb

Any help would be greatly appreciated.

Thanks,

Chip

+2  A: 

Simple, use a GET parameter:

def index 
    if params[:use]
       @listings = Listing.find(:all, :conditions => {:general_use => params[:use]})
    else
       @listings = Listing.all
    end
end

In your view, add a link to ?use=industrial and you're all set.

Jacob Relkin
Thanks Jacob! It is working perfectly. You can see your code in action at http://www.commercialonerealtors.com/listings where I have links to Industrial, Land, Multi-Family, etc. I really appreciate your help.
Chip Ashby
@Chip, You're welcome. Can you accept my answer by clicking on the hollow checkmark to the left? Thanks!
Jacob Relkin