Rails: searching filter model attribute - ruby-on-rails

I need to add filter search feature in my search form.
It's something like a social network. My app has societies, and a societies have categories (fashion, press, factory, ecc). I need my search to filter those categories.
Since I saw there are a plenty of search gems (solr, thinking sphinx, elasticsearch, sunspot) and look through all of them is a bit overwhelming have anybody accomplished such a task?
Any suggestion?
Things the search form should perform:
1) Search for a Society Name (And I can handle this with Sunspot)
2) Filter all Society by category chosen by user in a select tag (something like this)
3) And if both the text field and the select are filled, search for the name only in the category chosen

A great gem that I found recently is Ransack, which Ryan Bates has a RailsCasts for.
Here is an example of how it's used.
<%= search_form_for #query do |f| %>
<!-- filter: `society_name` contains text -->
<%= f.label :society_name_cont %>
<%= f.text_field :society_name_cont %>
<!-- filter: `category_id` equals -->
<%= f.label :category_id_eq %>
<%= f.collection_select :category_id_eq, Category.all, :id, :name %>
<% end %>
If you're unfamiliar with collection_select the Rails Guides will help <http://guides.rubyonrails.org/form_helpers.html#making-select-boxes-with-ease>.

Related

How Ransack search field with multiple different predicates work?

I m creating a filter which searches the three query as using Ransack predicates such as _matches_all and _cont and _eq.
the same question address not found the answer link
I have tried the solution below but this doesn't work.
<%= f.label :category_name_matches_all_or_schedules_county_eq, "search" %>
<%= f.search_field :category_name_matches_all_or_schedules_county_eq %>

Form Design: How to best display/add different statuses?

I am working on a rails form. Essentially, a person can have multiple statuses and switch between the different statuses. In database table, the display will be simple as follows:
status start_date end_date
work 1/1/15 1/10/15
sick 1/11/15 2/15/15
work 2/16/15 3/15/15
sick 1/15/15 1/14/15
I need to prompt user to input these information. I have made a status class which belongs to a person class. So basically, these fields will be a part of nested forms.
My question is: How can I dynamically display these information to make forms elegant and clean to use?
Thanks!
If I understood your domain, your Person has many Status, right?
The simplest way to do it is use the gem cocoon. Your view will look like this:
<%= form_for #person do |person_form| %>
<%= person_form.input :name %>
<%= person_form.fields_for :statuses do |status_form| %>
<%= status_form.field :start_date, :end_date %>
<!-- cocoon's method to dynamically add nested forms -->
<%= link_to_add_association 'add status', person_form, :statuses
<% end %>
<% end %>

Ransack search by a selected column dynamicaly

i want to do a search with Ransack in a User model, but i need a select with the attributes of the model as options to perform the search for that model. So if i have as attributes name, last_name, email the select should have this fields to be selected and next to it an input field to write the text that i want to search in the selected column or attribute for the User model.
Is there a way to do it with Ransack in Rails 4.1?
I think that could be something like this
<%= search_form_for #q do |f| %>
<%= f.select :selected_field, collection_of_attributes %>
<%= f.search_field :user_selected_field_cont %>
<%= f.submit %>
<% end %>
Any help will be appreciated :)
Try using ransack's attribute_select. http://railscasts.com/episodes/370-ransack?view=asciicast

Rails page search form: default parameters

I have two Rails sites, Bargain Stock Funds (http://www.bargainstockfunds.com) and Doppler Value Investing (http://www.dopplervalueinvesting.com).
Bargain Stock Funds now has a search feature that allows users to obtain a list of funds meeting certain criteria. (The URL is http://www.bargainstockfunds.com/funds .) Doppler Value Investing also has a search feature that allows users to obtain a list of stocks meeting certain criteria. (The URL is http://www.dopplervalueinvesting.com/stocks/ .) Both sites use the Ransack gem to provide the search feature and the Kaminari gem to paginate the results.
Is there a way I can configure the search engines so that they are pre-loaded with certain criteria already set? For Bargain Stock Funds, I want the search engine pre-configured to exclude funds with a load_front or load_back value greater than 0.0%, and I want the results sorted with respect to the value of the pcf parameter. For Doppler Value Investing, I want the search engine pre-configured to exclude funds with a pass_legit value of false or a pass_quality value of false, and I want the results sorted with respect to the value of the dopeler_pb parameter. These pre-configured settings would save the user the trouble of having to hunt around for the criteria in the drop-down menus.
The source code for the search form for Bargain Stock Funds is:
<%= search_form_for #search, url: search_funds_path, method: :post do |f| %>
<%= f.condition_fields do |c| %>
<%= render "condition_fields", f: c %>
<% end %>
<p><%= link_to_add_fields "Add Conditions", f, :condition %></p>
<div class="field">
Sort:
<%= f.sort_fields do |s| %>
<%= s.sort_select %>
<% end %>
</div>
<div class="actions"><%= f.submit "Search" %></div>
<% end %>
The source code for the search form for Doppler Value Investing is quite similar. (Just replace "funds" with "stocks".)
Not sure I'm completely following. By "search engine" do you mean Google? Or do you mean your applications searching logic?
If the latter, this may help: https://stackoverflow.com/a/14553363/1797331
I'll have to check out your sites. I was developing something similar, focused on stock fundamentals -- but haven't done much with it lately. You might inspire me.

Posting to database from dropdown menu Rails

I have created a dropdown menu that is pulling Cities and Countries from two database tables (named citie and country).
I am using the following collection_select tag:
<section class="field">
<%= f.label :city %>
<%= f.collection_select(:id, Citie.all, :id, :city) %>
</section>
But when I submit my form nothing is being posted into my jobs table (the form is to generate a new job).
I have searched to find a solution for this and am sure I am just missing a small part but can't seem to figure out what it is and why it's not working.
Any advice and a solution would be much appreciated! Thanks
I'm not terribly familliar with collection_select but shouldn't you give the association name as the first argument? e.g.
<%= f.collection_select(:city, Citie.all, :id, :city) %>

Resources