开发者

How do I create a link to a list of records where field X contains Y in Ruby on Rails?

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

def index
 @listings = Listing.all
end

to...

def index
 @listing开发者_如何学JAVAs = 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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜