How Ransack search field with multiple different predicates work? - ruby-on-rails

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

Related

Ransack search for price with Money-rails

I'm using Ransack to add a search function to my rails app and I'm also using Money-rails gem to handle price and currency.
I want to enter a price in Dollars in the Ransack search field and then it converts it to cents in order to find it in the relevant table.
So basically the problem is how to convert the input form Dollars to cents before submitting the search with Ransack.
Search form:
<div class="form-group">
<%= f.label :price_cents_gteq, "Price between" %>
<%= f.text_field :price_cents_gteq %>
<%= f.label :price_cents_lteq, "And" %>
<%= f.text_field :price_cents_lteq %>
</div>
Thanks a lot in advance!
Custom predicate can solve the problem.
You can use formatter proc to convert dollar to cents. Problems can arise when you will display ransack object value of price.
Update #1
Or even better, you can solve it with ransacker class method.
Update #2
Here is the raw solution with model and ransaker method:
# model
ransacker :price_money, type: :integer, formatter: proc { |dollars| dollars * 100 } do |p|
p.table[:price_cents]
end
# form in view
<%= f.number_field :price_money_gteq %>
<%= f.number_field :price_money_lteq %>

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: how to use language list gem with select_tag in a form?

I'm in the middle of trying to create a form where one of the questions is to choose a specific language. I'm trying to use the language list gem here: https://github.com/scsmith/language_list . However, the documentation doesn't really show me how I would combine the list with a select_tag.
<%= form_for users_path, :method => "get" do %>
<%= label_tag "Select Country" %> <br>
<%= country_select(:user, :country, [], :include_blank => true) %> <br>
<%= label_tag "Language spoken" %>
<%= select_tag "Language", options_from_collection_for_select(LanguageList::COMMON_LANGUAGES, "id", "name") %>
<%= label_tag "City" %> <br>
<%= text_field_tag(:city) %>
<% end %>
gives me the error
undefined method 'id' for afr (af) - Afrikaans:LanguageList::LanguageInfo
on the line
<%= select_tag "Language", options_from_collection_for_select(LanguageList::COMMON_LANGUAGES, "id", "name") %>
Can anyone help me?
Don't use "id". The docs say you should use iso_639_1 (or iso_639_3 if you want 3-letter codes)
<%= select_tag "Language", options_from_collection_for_select(LanguageList::COMMON_LANGUAGES, "iso_639_1", "name") %>
The LanguageList class seems to return a hash of LanguageInfo instances, having attributes like name, type and code -- `options_from_collection_for_select' expects its first parameter to be the collection (the hash in this case), the second a method that will return the value you want to identify the item, and the third is a display string.
So when someone selects a language, what are you going to store in the database? Probably one of the codes, right? So in this were true, you would make the second argument a method that an instance of the collection would respond to, which (reading the source code of the gem) is either iso_639_1 or iso_639_3. name should already work.
So if you replace id with one of those two iso_nnn_n values, then the form should display. To actually save the language code in the database, you'll need a column in your database for it, which you may already have as language.
Sometimes it's make a lot of sense to store gem's data dump in the database.
here is sample with postgresql and rails https://github.com/serghei-topor/import-language-list-into-db-rails-sample
here is csv file of gem's data dump https://github.com/serghei-topor/language-list-csv
And select_tag will look like:
<%= select_tag "Language", options_from_collection_for_select(Language.where(is_common:true).order(:name), "id", "name") %>

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

Rails: searching filter model attribute

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

Resources