rails autocomplete tags separated by commas using regex

前端 未结 2 892
礼貌的吻别
礼貌的吻别 2021-02-04 17:48

I am using the old auto_complete 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

2条回答
  •  遥遥无期
    2021-02-04 18:05

    I'd look into the options for the text_field_with_auto_complete 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_complete 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 autocomplete plugin.

    The JS you'll want to split the string on commas 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 commas, eating extra whitespace.

提交回复
热议问题