admin管理员组

文章数量:1323029

I am using the old auto_plete plugin in conjunction with the acts as taggable on gem in an attempt to basically replicate the tagging behavior of Stack Overflow itself! I am more or less doing what is described in this rails cast. For single tags, this works great. However, I would really like to make it so that every time the user enters a space or a ma (much like on Stack Overflow), the autoplete will start anew. I'd imagine there is some way to do this via regex, but I'm not sure how to go about applying this behavior to the text_field (I'd imagine using JavaScript to "restart" the autoplete, but admittedly I am fairly weak when it es to JavaScript. This is what my view looks like:

<%= text_field_with_auto_plete :business, :tags, {}, { :url => formatted_businesses_path(:js), :method => :get, :with => "'search=' + element.value" } %>

My controller is very straightforward, simply saving the tags for that particular business.

If someone could point me in the right direction (As I'm not sure how to go about doing this) I would greatly appreciate it.

I am using the old auto_plete plugin in conjunction with the acts as taggable on gem in an attempt to basically replicate the tagging behavior of Stack Overflow itself! I am more or less doing what is described in this rails cast. For single tags, this works great. However, I would really like to make it so that every time the user enters a space or a ma (much like on Stack Overflow), the autoplete will start anew. I'd imagine there is some way to do this via regex, but I'm not sure how to go about applying this behavior to the text_field (I'd imagine using JavaScript to "restart" the autoplete, but admittedly I am fairly weak when it es to JavaScript. This is what my view looks like:

<%= text_field_with_auto_plete :business, :tags, {}, { :url => formatted_businesses_path(:js), :method => :get, :with => "'search=' + element.value" } %>

My controller is very straightforward, simply saving the tags for that particular business.

If someone could point me in the right direction (As I'm not sure how to go about doing this) I would greatly appreciate it.

Share Improve this question edited Jul 14, 2011 at 2:43 Lance Roberts 22.9k32 gold badges114 silver badges132 bronze badges asked Dec 20, 2010 at 3:48 goddamnyouryangoddamnyouryan 6,90615 gold badges57 silver badges108 bronze badges 2
  • Did you ever arrive at a solution? I need to do the exact same thing. – ardavis Commented Jun 3, 2011 at 12:24
  • @ardavis: even if this is a little bit too late for you, take a look into the option :tokens => [",", " "]. control.js offers "Tokenized incremental autopletion" using that. – evnu Commented Jul 24, 2011 at 16:13
Add a ment  | 

2 Answers 2

Reset to default 10

I know this is old, but to recreate this behavior I used rails3-jquery-autoplete with acts-as-taggable-on. They work very nicely and easily together.

// Model
class Foo < ActiveRecord::Base
  acts_as_taggable_on :tags
end

// Controller
class FoosController < ApplicationController
  autoplete :tag, :name, :class_name => 'ActsAsTaggableOn::Tag'
  ...
end

// Routes
resources :foos do
  collection do
    get :autoplete_tag_name
  end
end

//View
<% form_for :foo do |form| %>
  <%= form.label :tag_list, "Tags" %>
  <%= form.autoplete_field :tag_list, autoplete_tag_name_foos_path, :"data-delimiter" => ', ' %>
<% end %>

Hope that helps someone.

I'd look into the options for the text_field_with_auto_plete helper. If it doesn't support what you need, I'd ditch it in favor of something you have more control over. My experience with helpers/plugins like this is that they only save you time if you're doing exactly what they expect you to do. If you need anything custom, you'll incur more pain trying to work around them than they're worth.

To ditch the text_field_with_auto_plete helper, look at the HTML and JS that it generates in the rendered page. Copy and paste that, then modify it to do what you need. You can still use the controller side of the autoplete plugin.

The JS you'll want to split the string on mas will look something like this:

var tags = $('#myTextInput').value();
var splitTags = tags.split(/\w*,\w*/);

JS regexen are pretty similar to Ruby's. That regex will split on mas, eating extra whitespace.

本文标签: javascriptrails autocomplete tags separated by commas using regexStack Overflow