Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autocomplete source is an array #66

Open
SupremeA opened this issue Jul 11, 2016 · 6 comments
Open

Autocomplete source is an array #66

SupremeA opened this issue Jul 11, 2016 · 6 comments

Comments

@SupremeA
Copy link

I want to use rails-jquery-autocomplete but my source is a dynamic array from a field in my model. The documentation only talks about the source being separate model instances. Is there a way to make it use an array and look at each array item as a separate object when querying the source?

@bigtunacan
Copy link
Collaborator

Not entirely sure based on the level of detail you have provided here, but this sounds like you may just need to override the get_autocomplete_items method for custom behavior.

You can see an answer I posted on Stack Overflow detailing how this works.

http://stackoverflow.com/questions/30624357/using-extra-search-field-with-rails-jquery-autocomplete/33744215#33744215

@SupremeA
Copy link
Author

@bigtunacan
Copy link
Collaborator

Did you look in detail at the answer I provided to the other question on Stack Overflow? The question may be different, but the solution to your problem should be the same.

Out of the box the gem solves the issues of doing lookups directly off the models since that is the most common use case, but the flexibility to get a custom result is available by overriding the get_autocomplete_items with your custom behavior.

I also noticed in your Stack Overflow question you mentioned you are trying to follow along with a RailsCast video while implementing this. That video is of some old unmaintained gem that has no relation to this project.

@SupremeA
Copy link
Author

SupremeA commented Jul 16, 2016

One more issue. I did as you suggested and made a custom get_autocomplete_items method but I keep getting the error

NoMethodError - undefined methodid' for #<String:0x007fd48c76`

Here is my method. Any idea why? Can't find any docs about this really. My strings don't have ids.

   def get_autocomplete_items(parameters)
          @transaction = current_tenant.transaction
          @trans_array = @transaction.trans_names.find_all { |x| /#{Regexp.escape(params[:term])}/i =~ x }
         render json: @trans_array
   end

@bigtunacan
Copy link
Collaborator

@SupremeA Are you able to replicate it on a git repo that I can see? I don't think there is enough information to help debug that particular issue otherwise.

@SupremeA
Copy link
Author

@bigtunacan I can't replicate it on a git repo, but I think my issue is coming from the Source Code here. Should i be able to overwrite this method to not ask for an id? Or would it be easier to make another model and pass my array to there and let the gem autocomplete from that model. Im trying to figure out the path of least resistance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants