简体   繁体   English

使用下拉列表在Ruby on Rails中过滤数据库结果

[英]Filtering Database results in Ruby on Rails using dropdown

Respected ppl ... 尊敬的人...

Im really new with rails so i need your valuable help ... 我真的对Rails很陌生,所以我需要您的宝贵帮助...

I have a database view in my db called "graph_hospital_vacant_by_band" 我的数据库中有一个数据库视图,名为“ 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)

whose model is : 其模型是:

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

and controller is as follows : 控制器如下:

class GraphHospitalVacantByBandsController < InheritedResources::Base
end

And view is as follows : 并查看如下:

<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>

Which currently shows this 目前显示哪个

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

I need a dropdown above with the availaible specialisations such that on selecting the particular specialisation the nos,hospitalband for that specialisation is displayed ... 我需要在上面提供可用专长的下拉列表,以便在选择特定专长时显示该专长的编号,医院乐队...

I have looked around and experimented a lot but in vain .... 我环顾四周并做了很多尝试,但徒劳无功.....

Im not getting how should i update my model,and controller to handle the passed parameters .... 我没有得到我应该如何更新我的模型和控制器来处理传递的参数....

Thanx very much ... 非常感谢...

Sincere Regards -Sky 真诚的问候-天空

You can provide filters by using a combination of scopes and where clauses, but it might be easiest to look towards a gem that provides this type of functionality. 您可以通过结合使用范围和where子句来提供过滤器,但是最容易想到提供这种功能的gem。

Two popular options are Squeel and Ransack . 两种流行的选择是SqueelRansack Squeel provides enhancements to how you can search for records using the built-in methods of ActiveRecord. Squeel增强了使用ActiveRecord的内置方法搜索记录的方式。 Ransack uses a separate "search form" that you can use to filter the records you're displaying. Ransack使用一个单独的“搜索表单”,您可以使用它过滤正在显示的记录。

If you use either of those gems you'll be updating the controller, adding the search/filter logic and the view to add the search/filter options. 如果您使用这些gem之一,则将更新控制器,添加搜索/过滤器逻辑和视图以添加搜索/过滤器选项。

In your controller you'll add 在您的控制器中,您将添加

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

In your view you'll need to add a search form: 在您看来,您需要添加搜索表单:

<%= 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 %>

This is a pretty basic search form, providing empty text fields accepting values. 这是一个非常基本的搜索表单,提供了接受值的空文本字段。 For more polish you'd probably want to use selects and provide acceptable values, but this at least gets you started. 要获得更多润色效果,您可能希望使用选择并提供可接受的值,但这至少可以帮助您入门。

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

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