Note that there are some explanatory texts on larger screens.

plurals
  1. POJquery TokenInput filtering search query with params[:q] not matching first char
    primarykey
    data
    text
    <p>I am trying to create a token field using the TokenInput plugin to add members (only the friends of the user creating the group) to a group.</p> <p>So, I have my friendships controller code here:</p> <pre><code>class FriendshipsController &lt; ApplicationController def index @friend_list = @current_user.friends.map { |f| {:id =&gt; f.id, :name =&gt; f.first_name + ' ' + f.last_name}} respond_to do |format| format.html # index.html.erb format.json { render json: @friend_list } end end </code></pre> <p>This returns an array, like below, at the url "/friendships.json":</p> <pre><code>[{"id":6,"name":"John Smith"},{"id":7,"name":"Jane Doe"}] </code></pre> <p>Then, I have my form, with the jquery for TokenInput below it:</p> <pre><code> &lt;%= form_for(@group) do |f| %&gt; &lt;div class="field"&gt; &lt;%= f.label :group_name, "Name" %&gt;&lt;br /&gt; &lt;%= f.text_field :group_name %&gt; &lt;/div&gt; &lt;div class="ui_widget"&gt; &lt;%= f.label :member_tokens, "Members" %&gt;&lt;br /&gt; &lt;%= f.text_field :member_tokens %&gt; &lt;/div&gt; &lt;div class="actions"&gt; &lt;%= f.submit %&gt; &lt;/div&gt; &lt;% end %&gt; &lt;script&gt; $(function() { $('#group_member_tokens').tokenInput("/friendships.json"); }); &lt;/script&gt; </code></pre> <p>My question is, how do I filter the array at /friendships.json? I have tried doing the following in my FriendshipsController:</p> <pre><code>friend_list = @current_user.friends.map { |f| {:id =&gt; f.id, :name =&gt; f.first_name + ' ' + f.last_name}} @friend_list = friend_list.where("name LIKE ?", "%#{params[:q]}%") </code></pre> <p>... but this only returns an empty array at /friendships.json.</p> <p>If anyone could let me know why this is returning an empty array I would greatly appreciate it.</p> <p><strong>UPDATE:</strong></p> <p>I tried doing this:</p> <pre><code> @friend_list = @user_name.friends.where("name LIKE ?", "%#{params[:q]}%") @friend_list = @friend_list.map { |f| {:id =&gt; f.id, :first_name =&gt; f.first_name + ' ' + f.last_name}} </code></pre> <p>... but when I add the code ".where("name LIKE?", "%#{params[:q]}%")" to the end of @friend_list = current_user.friends, then I no longer get any autocomplete results in the TokenInput field (it returns "No Results").</p> <p>When I manually enter the q param into the URL, for example "http://localhost:3000/friendships.json?q=k" ... I end up with an empty array. This is weird because a User with first_name Kevin is in my application. </p> <p>Here is what is outputted on my ruby server:</p> <pre><code>Started GET "/friendships.json?q=ke" for 127.0.0.1 at 2012-09-16 19:27:53 -0400 Processing by FriendshipsController#index as JSON Parameters: {"q"=&gt;"ke"} [...] User Load (0.7ms) SELECT "users".* FROM "users" INNER JOIN "friendships" ON "users"."id" = "friendships"."friend_id" WHERE "friendships"."user_id" = 2 AND (first_name LIKE '%ke%') [...] </code></pre> <p>HOWEVER, I just tried typing in "evin" into the tokenInput text_field, and it found the user. For some reason the q parameter can't find the first letter of the user's first_name. Anyone have any thoughts on this?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload