Rails gem rails3-jquery-autocomplete how to scope by user

眉间皱痕 提交于 2019-11-27 23:07:51
deb

In posts_controller:

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

I'm first calling the original get_autocomplete_items method, and then filtering out the results by current_user.id.

This question helped: Rails 3: alias_method_chain still used?

I had a similar problem I solved thanks to the answers above.

My autocomplete also worked against a User model, but I needed to restrict the results to the user's institution (Institution has many :users). My controller creates an @institution instance variable that is accessed in the view.

Although the get_autocomplete_items method cannot directly access the instance variable, I found that the data CAN be passed to autocomplete as a parameter (note: I use the simple_forms gem, so the input call looks a little different than the standard rails syntax).

In my view:

<%= f.input :email,  :url => autocomplete_user_email_institutions_path(:institution_id=>@institution.id.to_s), :as => :autocomplete %>

In my controller:

  autocomplete :user, :email, :extra_data => [:first_name, :last_name]

  def get_autocomplete_items(parameters)
    super(parameters).where(:institution_id => params[:institution_id])
  end

My autocomplete list is now scoped to just the users who work for a particular institution.

deb's answer works for me. The code can be cleaned up a bit:

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


There is small update to code for those who have having trouble with super method.because of dynamic dispatch it above code need to replaced as below:

def get_autocomplete_items(parameters)
 items = super(parameters)
 items = items.where(searchable: true)
end

to this:

def get_autocomplete_items(parameters)
 items = active_record_get_autocomplete_items(parameters)
 items = items.where(searchable: true)
end

Reference: https://github.com/crowdint/rails3-jquery-autocomplete/issues/278

To answer the question posed by @ctilley79, multiple autocompletes is not a problem because, in addition to the possibility of passing more values in the params hash, you also have access to the autocomplete parameters. On my form (as an example), I have both a City and a Zip autocomplete. I need to restrict the City to those in a certain state. So my controller action looks like this:

def get_autocomplete_items(parameters)
  if (parameters[:model] == City)
    super(parameters).where("state_id" => params[:state_id])
  else
    super(parameters)
  end
end

You also have access to the method in case you need it. Do logger.debug on the parameters to see all that is available.

I faced a similar problem. Our site is multi-tenant, so everything needs to be scoped to the tenant.

To make this easier, I modified rails3-jquery-autocomplete to accept another option called :base_scope. It takes a string, that gets eval'd instead of using the model. All the other functionality works, so you can append additional scopes and where clauses if you need to.

My fork is here: https://github.com/GiveCorps/rails3-jquery-autocomplete

I am not sure that the tests i wrote prove it will always work. I just checked that it was using the scope instead of the model in the items method.

i would appreciate any thoughts on it. Not sure whether it merits a pull request.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!