Scoping the results for rails3 jquery autocomplete plugin

前端 未结 3 804
青春惊慌失措
青春惊慌失措 2020-12-10 17:05

I\'m using https://github.com/crowdint/rails3-jquery-autocomplete and it seems to be working. The only problem is lets say I have a field performing an autocomplete on all p

相关标签:
3条回答
  • 2020-12-10 17:39

    Even though I havent personally tested, I think normal scope method in rails3 should work. As after all scope is also a method same as "funky_method"

    write a scope

    in your model

    scope :funky_method, lambda { |param1| :conditions => {:column = param1} }

    and in your controller

    autocomplete :brand, :name, :display_value => :funky_method

    should work, try and see..

    cheers

    sameera

    0 讨论(0)
  • 2020-12-10 17:52

    You should overwrite the method get_item, for example in these case I'm filtering the list of users to consider to just the last one:

    class UsersController < ApplicationController
        autocomplete :user, :email, :full => true
    
        def index
            @users = User.all
        end
    
        def get_items(parameters)
            [User.last]
        end
    

    I think that one of the last commits, changed the method (#get_items) name. Check you version of autocomplete.rb file.

    Check it here: https://github.com/crowdint/rails3-jquery-autocomplete/commit/b3c18ac92ced2932ac6e9d17237343b512d2144d#L1R84.

    0 讨论(0)
  • 2020-12-10 17:57

    You can override get_autocomplete_items, but first use super to call the original get_autocomplete_items method, and then filter the results by current_user.id (or post.user.id if the owner of the post is not the current user)

     def get_autocomplete_items(parameters)
        items = super(parameters)
        items = items.where(:user_id => current_user.id)
      end
    

    This way you can still all the other options that come with the gem. This should be done in your controller by the way.

    Another answer suggested the :scope option. I looked into that, but it is not the way to go in this case, you don't want to add "current_user" and things like that to your models.

    0 讨论(0)
提交回复
热议问题