RoR live-search (text_field_with_auto_complete) send

I have Films and Actors and Casts as a join model. To be more specific, "Casts" has movie_id, actor_id, and rolename. I want in the "Movies" form to add a live search to search for actors and "rolename" text_field and save them in "Casts". I don't know if text_field_with_auto_complete is the right choice, but I prefer not to use a lot of javascript because I am not familiar with it. I searched all over the internet to find something like this without any results. I managed to get him to work with "@ actors.each do", but he makes a very long list.

+4
source share
2 answers

This is not a plugin, but with a little jQuery magic you can use http://github.com/chadisfaction/jQuery-Tokenizing-Autocomplete-Plugin . The best part about this is that since it is pure JS in its implementation, you can create an AJAX call yourself in Rails and display only what you want. It even allows you to add a stylesheet to the drop-down list if you want to make it more like Facebook. In the controller, add a function so that the AJAX call returns the list of strings in JSON:

def taglist tags = [] sql = "SELECT id,name ... LIMIT 15" # Enter SQL here to produce a possible result set result = ActiveRecord::Base.connection.execute(sql) # Iterate over the hash values and push them into an array result.each { |field| tags.push( {"id" => field[0], "name" => field[1]} ) } result.free render :json => tags, :layout => false end 

In the view, add the following code:

 <%= javascript_include_tag 'jquery.tokeninput' %> <%= stylesheet_link_tag 'token-input-facebook' %> <script type="text/javascript"> jQuery(document).ready(function () { jQuery("#actors_role").tokenInput("/actors/rolesearch", { allowNewValues: false, canCreate: false, hintText: "Enter the actor name or role they played", }); }); </script> 
+2
source

See text_field_with_auto_complete inside form_for

In the action of the auto_complete controller, make sure that your SQL query restricts the names of participants using the passed parameter.

0
source

Source: https://habr.com/ru/post/1300872/


All Articles