简体   繁体   English

如何在Ruby on Rails中创建指向字段X包含Y的记录列表的链接?

[英]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
 @listings = Listing.where("general_use == 'Industrial'")
end

... in listings_controller.rb ...在listings_controller.rb中

The Listings index view shows a list of all Listings where the general_use field contains the word Industrial. 清单索引视图显示了所有清单的列表,其中general_use字段包含单词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 控制器\\ listings_controller.rb
  • helpers\\listings_helpers.rb 佣工\\ listings_helpers.rb
  • models\\listing.rb 型号\\ listing.rb
  • views\\listings\\index.html.erb 意见\\目录\\ index.html.erb
  • config\\routes.rb 配置\\ routes.rb中

Any help would be greatly appreciated. 任何帮助将不胜感激。

Thanks, 谢谢,

Chip 芯片

Simple, use a GET parameter: 简单,使用GET参数:

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. 在您的视图中,添加一个指向?use=industrial的链接,您已经准备就绪。

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

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