Scoping the results for rails3 jquery autocomplete plugin

前端 未结 3 811
青春惊慌失措
青春惊慌失措 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: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.

提交回复
热议问题