Rails select2 with filterrific gem - ruby-on-rails

I currently have a list of events that I want my users to be able to filter. Each event has a performer and I store the performers id for each event.
It isn't very user friendly for my users to be able to search by the user id as it won't mean much to them so I want them to be able to search by the performers name.
I had previously created a simple search form that used select2 rails gem and the code in my view would look something like this:
<%= select_tag(:performer, options_from_collection_for_select(#performer,
:id, :profile_name, :selected => params[:performer]), :include_blank => true, id: "user", :class => "form-control") %>
<script type="text/javascript">
$(document).ready(function() {
$('select#user').select2({
allowClear: true,
placeholder: "Choose a profile name",
});
});
This would allow my users to be able to search and select a name and it would find the relevant events that related to their user_id.
I now want to use the filterrifc gem to make the search update in realtime as the user is typing and to display the results but I cannot work out how to modify my code to work alongside the gem.
If I follow the example in the documentation for a simple text search, the scope in the model would look like this:
scope :performer, lambda { |query|
where("LOWER(events.performer) LIKE ?", "%#{query}%")}
But I cannot work out how to combine the select2 gem with the filterrific gem to get the result that I want.

This seems to have done the trick.
<%= f.select :performer, options_from_collection_for_select(#performer, :profile_name,:name), {:include_blank => true}, id: "user", class: "form-control", data: { placeholder: "Choose a performer" } %>

Related

Rails - unable to convert simple select to chosen field

What I have
I have a simple select field on a view, its not part of any form, selecting any option sends a remote request to the route.
There are a bunch of applications in a view listed in table and each application has this dropdown which user can use to assign a tag to the application. The code of this select_tag is as following.
<%= select_tag 'application_tag',
options_from_collection_for_select(HiringTag.order(:name), :id, :name, application.tag),
prompt: "Assign a tag to the application",
class: "form-control input-block-level chzn-select",
id: "hiring_tag_dropdown",
data: {
remote: true,
url: "applications/"+application.id.to_s+"/assign_tags",
method: 'post'
}
%>
Right now this works fine as it is and tag is being assigned to the application.
What I am trying to do
I am trying to convert this select_tag to multi select field using collection_select and the gem I am using is gem 'chosen-rails'. Looking at the documentation and several other blog articles it seemed like a straight forward job but I just cant get the field to appear the way it should. This is what I have done.
<%= collection_select :application_id, :application_tag, HiringTag.order(:name), :id, :name, {}, {include_blank: true, multiple: true, class: 'form-control input-block-level chzn-select'} %>
This is what the field looks like in view
and this is how i am expecting it to look like
I also added the javascript part but that did not help either
$('.chzn-select').chosen();
Question
What am i doing wrong here :)

Adding a tag if it doesn't exist in rails chosen rails

I have implemented the functionality of adding tags to a post in Rails using chosen-rails and acts-as-taggable gems. I want to further enhance the functionality by making user to create new tag if it doesn't exist.
form.html.slim
.form-group.text-center
= f.label :tag_ids, "Tags"
= f.collection_select :tag_ids, Tag.order(:name), :id, :name, {}, {multiple: true}
I need to add a new tag if it doesn't exist.
Ok with Selectize.js, Acts_as_Taggable, & SimpleForm:
Firstly, change your input from collection_select to a normal string input. Selectize.js will separate all of the values into tags anywhere there is a comma. This happens behind the scenes so as people add tags, it's actually inserting a long string into your database with whatever delimiter you supply. Then you need to add an id to that field so you can initialize selectize such as:
<%= f.input :nationalities, as: :string, placeholder: "Nationalities", input_html: { id: 'nationality-input'} %>
Then initialize selectize.js:
#The following line gets all tags ever posted for a user with the context 'nationalities' which we will use as options.
<%=nations = ActsAsTaggleOn::Tagging.includes(:tag).where(context: 'nationalities').uniq.pluck(:id, :name)
$(document).ready(function() {
$('#nationality-input).selectize({
delimiter: ',',
persist: true,
allowEmptyOption: false,
options: [
<% nations.each do |nat| %>
{text: '<%=nat[1] %>', value: '<%=nat[1]%>' },
<% end %>
searchField: 'text',
create: function(input) {
return {
value: input,
text: input
}
}
});
});
Make sure you have acts_as_taggable setup properly and that the corresponding model includes it. Then in your controller just save the whole string with commas and all and allow selectize to reformat it on views automagically.

How to implement multi select in a independent table in Rails?

My problem is that I have, for example, Product, Category and ProductCategory.
ProductCategory makes possible for a Product have several Categories
I would like to implement this using Select2 (http://ivaynberg.github.io/select2/) using the select2-rails gem (https://github.com/argerim/select2-rails)
I already know how to relate the models but I can't figure out how to implement the Select2 specific code.
EDIT:
Now I see that my problem was not much about select2, so I added this comment and changed the title hoping that it can help somebody else
Now I see that my problems were not about select2 but to do a multi select.
The code in the _form.html.erb that make it work is this one:
<%= f.label :category_id %>
<%= f.collection_select :category_ids, Category.order(:name), :id, :name, {:selected => #product.category_ids, :include_blank => true}, {:class => 'col-xs-12 col-md-7 padding_15', :multiple => true} %>
I also included :category_ids in the attr_accessible on models/product.rb
And the select2 specific, I included in a .js file
$(document).ready(function() {
$('#product_category_ids').select2();
});
I'm including these links as they helped me but pay attention on the differences depending on the Ruby/Rails versions
http://www.dzone.com/snippets/using-mutiple-collection
http://www.alethe.com/brad/2009/10/multiple-select-list-in-rails/
Just to let you know, unexpectedly, if this collection_select is the last line in my form, some form fields are disabled although there is nothing that states this in the source. Changing the order this problem doesn't exist.
I also don't know why the look is a little different than the other fields (I'm using Bootstrap 3)

ruby on rails select collection, filter results from array [enumerable]

current i am trying to restrict the information feed into an option select field to only display the criteria i have selected. with the code below this seems to be working
= select("schedule", :selected_players, User.where(:team_id => current_user[:team_id]) { |p| [full_name(p), p.id] }, {:include_blank => 'None', :prompt => 'Add Players to Lineup'}, :multiple => "multiple")
the issue is that this code is display an array field type i.e #<User:0xa559830>
how do i get it to display the actual users name?
Define .to_s method in model
Like here
https://github.com/roolo/mwstt/blob/master/app/models/project.rb#L7
Also all the mapping and searching logic should be placed in model as method which you'll just call in view, or prepare it in controller!

Ruby On Rails - Showing the default option with collection_select dependent on params

I'm using an out-dated version of rails (2.2).
I have a page which has a search filter. When I filter the options, I would like the Dropdown boxes to default to the filters I selected. The filters are being stored as parameters in the URL. i.e. filter[Issue+Header]=test&filter[in4User]=1&filter[Module]=3
What I search:
http://i.stack.imgur.com/r804l.png
What I currently see when page loads (as you can see, text boxes are re-populated, but dropdowns are not):
http://i.stack.imgur.com/G83X8.png
What I want to see when page loads:
http:// [remove_this_space] i.stack.imgur.com/r804l.png
Example of a collection_select I am using:
<%= collection_select(:filter, "Client", Client.find(:all, :conditions => ['status = 0']), :ClientID, :Name, :include_blank => true) %>
What you need to do is pass in the :selected option into collection select, and pass the appropriate param as the value, so something like:
<%= collection_select(:filter, "Client", Client.find(:all, :conditions => ['status = 0']), :ClientID, :Name, :include_blank => true, :selected => params[:filter]) %>
That should select the client, assuming that the Client is in the collection.

Resources