2013-03-14 44 views
0

尊敬的脂肪酶的Rails ...过滤数据库导致的Ruby on使用下拉

我真的有栏杆,所以我需要你的宝贵帮助新...

我在我的DB数据库视图称为“graph_hospital_vacant_by_band “

mysql> desc graph_hospital_vacant_by_band; 
+-------------------+--------------+------+-----+---------+-------+ 
| Field    | Type   | Null | Key | Default | Extra | 
+-------------------+--------------+------+-----+---------+-------+ 
| specialisation_id | int(11)  | NO |  | NULL |  | 
| specialisation | varchar(255) | YES |  | NULL |  | 
| nos    | bigint(21) | NO |  | 0  |  | 
| hospitalband  | varchar(255) | NO |  | NULL |  | 
+-------------------+--------------+------+-----+---------+-------+ 
4 rows in set (0.00 sec) 

其模型是:

class GraphHospitalVacantByBand < ActiveRecord::Base 
self.table_name = 'graph_hospital_vacant_by_band' 
end 

和控制器是一个小号如下:

class GraphHospitalVacantByBandsController < InheritedResources::Base 
end 

而且视图如下:

<table class="table table-bordered"> 
    <thead> 
    <tr> 
     <th>Specialisation</th> 
       <th>No.</th> 
     <th>Hospital Band</th> 
    </tr> 
    </thead> 
<tbody> 
<% @graph_hospital_vacant_by_bands.each do |graph_hospital_vacant_by_band| %> 
    <tr> 
      <td><%= graph_hospital_vacant_by_band.specialisation %></td> 
      <td><%= graph_hospital_vacant_by_band.nos %></td> 
      <td><%= graph_hospital_vacant_by_band.hospitalband %></td> 
    </tr> 
<% end %> 
</tbody> 
</table> 

目前示出了本

http://i.stack.imgur.com/v7Jwt.png

我需要与上面所述availaible特下拉,使得在选择所述特殊专业nos,该专业的医院带显示...

我环顾四周,并尝试了很多,但白白....

林没有得到我应该如何更新我的模型,和控制器来处理传递的参数....

感谢名单非常...

诚挚的问候 -sky

+0

请帮助脂肪酶... – SkyKOG 2013-03-14 17:05:59

回答

1

您可以通过使用范围和where子句的组合提供过滤器,但它可能是最简单的把目光投向提供这类功能的瑰宝。

两个受欢迎的选项是SqueelRansack。 Squeel使用ActiveRecord的内置方法提供了如何搜索记录的增强功能。 Ransack使用一个单独的“搜索表单”,您可以使用它来过滤正在显示的记录。

如果您使用其中任何一种宝石,您将更新控制器,添加搜索/过滤器逻辑和视图以添加搜索/过滤器选项。

在您的控制器,您将添加

def index 
    @q = GraphHospitalVacantByBand.search(params[:q]) 
    @graph_hospital_vacant_by_bands = @q.result(:distinct => true) 
end 

在你看来,你需要添加一个搜索表单:

<%= search_form_for @q do |f| %> 
    <%= f.label :specialisation_cont %> 
    <%= f.text_field :specialisation_cont %> 
    <%= f.label :nos_eq %> 
    <%= f.text_field :nos_eq %> 
    <%= f.label :hospitalband_cont %> 
    <%= f.text_field :hospitalband_cont %> 
    <%= f.submit %> 
<% end %> 

这是一个非常基本的搜索表单,提供空的文本字段接受价值。为了获得更多波兰语,你可能想要使用选择并提供可接受的值,但这至少可以让你开始。

+0

感谢名单非常多先生:) ......你是个明星... 它完美:) .... http://i.imgur.com/yGvnTcT.png 唯一的是我如何使这个文本框下拉? ...那真的很棒.... Thnx再次.... – SkyKOG 2013-03-15 06:08:32

+0

Thanx所有你的帮助:) ...我真的希望我能代表你:) :) 我终于用这个获取下拉列表: <%= f.select:specialisation_id_eq,options_from_collection_for_select(GraphHospitalVacant.all, “id”,“Specialisation_name”,@ q.specialisation_id_eq)%> 其中GraphHospitalVacant包含不同的id专业化:) ... – SkyKOG 2013-03-15 07:31:25

+0

Rails有一个专门为此场景设计的表单助手,名为'collection_select'。 [在API上阅读更多内容](http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-collection_select) – 2013-03-15 13:15:51