Ransack search by a selected column dynamicaly - ruby-on-rails

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

Related

Ruby on rails. Ransack search associations

I'm getting a little stuck with a ransack search form.
I need to search through a model's association, and bring results back when name_cont BUT ONLY WHEN AN associated model's status == something.
I have a gig model with a has_many belongs_to relationship with a request model and user model. The request model has attributes status and the user model has attributes publicname. I want to search publicname_cont and only show results from the requests that have a status == "hired".
Currently my search brings back the results of publicname regardless or the request models status.
Gigs Controller:
#gigs = #q.result(distinct: false).notexpired.order(date: :asc).page(params[:page]).per(20)
Search form:
<%= search_form_for #q, url: welcome_index_url, remote: false do |f| %>
<%= text_field_tag :search, params[:search], class: 'loc-search', placeholder: "Location"%>
<%= f.search_field :locationname_cont, placeholder: "Bar name" %>
<%= f.search_field :requests_user_publicname_cont, placeholder: "Band name" %>
<%= f.submit %>
<% end %>
Currently, I can search requests_user_publicname_cont and it gives me all the results as expected where the parameters match.
I am looking for a way to search requests_user_publicname_cont AND requests_status_eq, ("hired")
I am not sure how to go about this, any help would be appreciated, thanks.
#q = Gig.joins(:requests).where(requests: { status: 'hired' }).ransack(params[:q])

Form without model

I want to let users select between two radio buttons, and then transfer that data to a controller where it will be used as a filter on a database. The filtered data from the database should then be displayed back on the page.
My form
<%= form_tag do %>
<%= radio_button_tag(:sex, "male") %>
<%= label_tag(:male, "I am male") %>
<%= radio_button_tag(:sex, "female") %>
<%= label_tag(:female, "I am female") %>
<%= submit_tag("Submit") %>
<% end %>
But I am unsure on how I can achieve this. I have created a model "Countries" that refers to a database that have a list of countries and some statistics related to gender within that country. Any suggestions?
I would suggest that you implement search functionality on the model "Countries".Railscast has a few casts on the subject.

Filter selection in Simple Form

I have this code in my form :
guidances/new.html.erb
<%= f.association :employee_department, as: :select %>
<%= f.input :guidance_supervisor do %>
<%= f.select :guidance_supervisor,
Employee.where('guidance_supervisor' => true).map(&:full_name) %>
<% end %>
and when selecting a department I need to make the employees who belongs to that department appears, I'm using simple_form , what is the way to make this happen ?
For this you can use dynamic drop-down concept. You can see the following post for dynamic drop down in rails:
Dynamic drop down in rails.

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

Creating a drop down list in rails

Can any one help me in creating a drop down list in rails. I'm having a user table with a role field and I want to create a drop down list with values manager, investigator, director for role.How I can extract this value to parms[:role] . I'm new to rails.
If you are using form_for (not form_tag), then it should go like this.
<%= form_for #user do |f| %>
...
<%= f.select :role, options_for_select(%w[manager investigator director]) %>
<% end %>
Then you'll have params[:user][:role] available in your controller.
Refer this for more info on select helper.

Resources