user984621
user984621

Reputation: 48453

Rails 3 - how to use autocomplete with textarea instead of classic text input?

I am playing with rails3-jquery-autocomplete gem and it works me pretty well. Here is an example, how looks my autocomplete input:

= f.autocomplete_field :interest, autocomplete_interest_users_path, :"data-delimiter" => ','

Because many times I need to write lots of words into the input, that's why would be better to use textarea instead of default input, but how to do that? Any ideas?

Thank you in advance.

Upvotes: 0

Views: 721

Answers (2)

Sid Krishnan
Sid Krishnan

Reputation: 451

Here's how I did it with jQuery:

Add this as a helper:

module ActionView
  module Helpers
    module FormHelper

      def autocomplete_text_area(object_name, method, source, options ={})
        options["data-autocomplete"] = source
        text_area(object_name, method, rewrite_autocomplete_option(options))
      end
    end
  end

  class ActionView::Helpers::FormBuilder
    def autocomplete_text_area(method, source, options = {})
      @template.autocomplete_text_area(@object_name, method, source, objectify_options(options))
    end
  end
end

This is how you would extend form helpers in Rails. What we're doing here is adding our own method which we can call in our view.

Now, add this to one of your javascript files:

 $('textarea[data-autocomplete]').railsAutocomplete()

This sets up the autocomplete js script to listen and respond to our textarea.

And you should now be able to do something like this in your view:

f.autocomplete_text_area :name, autocomplete_menu_item_name_menus_path, placeholder: "Menu Item Name", class: 'input-lg menu_item', size: "45x2"

Upvotes: 0

Steve Jorgensen
Steve Jorgensen

Reputation: 12341

Something like http://rightjs.org/ui/tags/demo perhaps? There's an article on making this work with Rails at http://st-on-it.blogspot.com/2011/04/making-tags-with-rails-and-rightjs.html .

Upvotes: 1

Related Questions