SkyKOG
SkyKOG

Reputation: 287

Filtering Database results in Ruby on Rails using dropdown

Respected ppl ...

Im really new with rails so i need your valuable help ...

I have a database view in my db called "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

https://i.sstatic.net/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

Upvotes: 0

Views: 2178

Answers (1)

Dan Reedy
Dan Reedy

Reputation: 1857

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.

Two popular options are Squeel and Ransack. Squeel provides enhancements to how you can search for records using the built-in methods of ActiveRecord. Ransack uses a separate "search form" that you can use to filter the records you're displaying.

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.

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.

Upvotes: 1

Related Questions