当没有模型时使用 Rails jQuery 自动完成

发布于 2025-01-07 21:06:56 字数 902 浏览 1 评论 0原文

jQuery 自动完成文档指出应该有两个模型,第一个是您要用于自动完成的模型,第二个是具有第一个模型(并附加到您的页面)的模型。但是,我没有第二个模型,只有一个带有表单元素的视图的控制器,其中一个是第一个模型(地点)的自动完成输入。

文档说我应该这样做

resources :parse do
get :autocomplete_venue_name, :on => :collection
end

,但这不会起作用,因为控制器“解析”没有模型。我尝试制作静态路由,但我需要 :collection 选项。我该如何解决这个要求?

编辑: 下面是来自 Rails+jquery 自动完成插件的代码。请注意,我有一个 ProductsController,但没有 Products 模型,因此我无法执行routes.rb 设置:

class Brand < ActiveRecord::基础 最终

要搜索的

   create_table :brand do |t|
     t.column :name, :string
   end

模型:具有所需自动完成形式的控制器:

class ProductsController < Admin::BaseController
   autocomplete :brand, :name
end

ROUTES.RB

resources :products do
   get :autocomplete_brand_name, :on => :collection
end

此方法仅在我有产品模型时才有效,而我没有。我有一个控制器,它显示带有自动完成输入的表单。

jQuery autocomplete docs state there should be two Models, the first being the model that you want to use for autocomplete, the second being the model that has the first Model (and is attached to your page). However, I don't have the second model, just a Controller w/view that has form elements, one of them an autocomplete input of the first Model (Venue).

The docs say I should do

resources :parse do
get :autocomplete_venue_name, :on => :collection
end

But that won't work since the controller 'Parse' doesn't have a model. I tried making a static route but I need that :collection option. How can I work around this requirement?

EDIT:
Below is the code from the rails+jquery autocomplete plugin. Note that I have a ProductsController but I don't have a Products model, so I can't do the routes.rb setup:

class Brand < ActiveRecord::Base
end

MODEL TO SEARCH ON:

   create_table :brand do |t|
     t.column :name, :string
   end

CONTROLLER WITH DESIRED AUTOCOMPLETE FORM:

class ProductsController < Admin::BaseController
   autocomplete :brand, :name
end

ROUTES.RB

resources :products do
   get :autocomplete_brand_name, :on => :collection
end

This approach works only if I have a Products model, which I do not. I have a Controller that is displaying a form w/autocomplete inputs.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小忆控 2025-01-14 21:06:56

我正在使用 jquery 多自动完成插件,您可以在其中选择一个或多个自动

完成值,如果没有模型,那么它将工作,例如

$(document).ready(function() {

     var availableTags = [];
    <%@tags.each do |tag|%>
        availableTags.push('<%=tag.name%>');
    <%end%>

    $('#tags').multicomplete({
        source: availableTags
    });

});

在我的情况下 @tags = Tag.all

但是您可以在控制器操作中给出任何其他值。其模型不存在

就像控制器

@tags = ["cute","beauty","amazing"]

我用于多重自动完成的代码如下,

(function($, $a, $p) { // getting $p as a parameter doesn't require me to "var $p=..." and saves a two bytes ;-) ("var " versus ",x" in argument list [when minifier is shrinking variables])
    $p = $a.prototype;
    $.widget('ui.multicomplete', $a, {
        // Search for everything after the last "," instead of the whole input contents
        _search: function(value) {
            $p._search.call(this, value.match(/\s*([^,]*)\s*$/)[1]);
        },
        // Overwrite _trigger to make custom handling for events while still allowing user callbacks
        // Setting my own callbacks on this.options or binding using .bind() doesn't allow me to properly handle user callbacks, as this must be called AFTER the user callbacks are executed (which isn't possible by bind()ing when this.options[] is set)
        _trigger: function(type, event, data) {
            // call "real" triggers
            var ret = $p._trigger.apply(this, arguments);
            // When its select event, and user callback didn't return FALSE, do my handling and return false
            if (type == 'select' && ret !== false) {
                // When a selection is made, replace everything after the last "," with the selection instead of replacing everything
                var val = this.element.val();
                this.element.val(val.replace(/[^,]+$/, (val.indexOf(',') != -1 ? ' ' : '') + data.item.value + ', '));
                ret = false;
            }
            // Force false when its the focus event - parent should never set the value on focus
            return (type == 'focus' ? false : ret);
        },
        _create:function() {
            var self = this;
            // When menu item is selected and TAB is pressed focus should remain on current element to allow adding more values
            this.element.keydown(function(e) {
                self.menu.active && e.keyCode == $.ui.keyCode.TAB && e.preventDefault();
            });
            $p._create.call(this);
        },
        _initSource: function() {
            // Change the way arrays are handled by making autocomplete think the user sent his own source callback instead of an array
            // The way autocomplete is written doesn't allow me to do it in a prettier way :(
            if ($.isArray(this.options.source)) {
                var array = this.options.source, self = this;
                this.options.source = function(request, response) {
                    response(self.filter(array, request)); // Use our filter() and pass the entire request object so the filter can tell what's currently selected
                };
            }

            // call autocomplete._initSource to create this.source function according to user input
            $p._initSource.call(this);

            // Save a copy of current source() function, than new source() sets request.selected and delegate to original source
            var _source = this.source;
            this.source = function(request, response) {
                request.selected = this.element.val().split(/\s*,\s*/);
                request.selected.pop(); // don't include the term the user is currently writing as selected
                _source(request, response);
            };
            // TODO: instead of overwritting this.source, I can overwrite _search which is easier, but than I'll have to repeat jQuery-UI's code that might change
        },

        // Like $.ui.autocomplete.filter, but excludes items that are already selected
        filter: function(array, request) {
            return $.grep($a.filter(array, request.term), function(value) {
                return $.inArray(value, request.selected) == -1;
            });
        }
    });
})(jQuery, jQuery.ui.autocomplete);

但您应该包括 。一些jquery自动完成js从那里下载文件

jquery.ui.core

jquery.ui.widget

jquery.ui.position

jquery.ui.autocomplete

I am using jquery multi auto complete plugin in which you can select one or multi auto

complete value and if there is no model then it will work e.g

$(document).ready(function() {

     var availableTags = [];
    <%@tags.each do |tag|%>
        availableTags.push('<%=tag.name%>');
    <%end%>

    $('#tags').multicomplete({
        source: availableTags
    });

});

in my case @tags = Tag.all

But you can give any other values in controller action.Whose model is not exist

Like in controller

@tags = ["cute","beauty","amazing"]

The code i am using for multi Autocomplete is below.

(function($, $a, $p) { // getting $p as a parameter doesn't require me to "var $p=..." and saves a two bytes ;-) ("var " versus ",x" in argument list [when minifier is shrinking variables])
    $p = $a.prototype;
    $.widget('ui.multicomplete', $a, {
        // Search for everything after the last "," instead of the whole input contents
        _search: function(value) {
            $p._search.call(this, value.match(/\s*([^,]*)\s*$/)[1]);
        },
        // Overwrite _trigger to make custom handling for events while still allowing user callbacks
        // Setting my own callbacks on this.options or binding using .bind() doesn't allow me to properly handle user callbacks, as this must be called AFTER the user callbacks are executed (which isn't possible by bind()ing when this.options[] is set)
        _trigger: function(type, event, data) {
            // call "real" triggers
            var ret = $p._trigger.apply(this, arguments);
            // When its select event, and user callback didn't return FALSE, do my handling and return false
            if (type == 'select' && ret !== false) {
                // When a selection is made, replace everything after the last "," with the selection instead of replacing everything
                var val = this.element.val();
                this.element.val(val.replace(/[^,]+$/, (val.indexOf(',') != -1 ? ' ' : '') + data.item.value + ', '));
                ret = false;
            }
            // Force false when its the focus event - parent should never set the value on focus
            return (type == 'focus' ? false : ret);
        },
        _create:function() {
            var self = this;
            // When menu item is selected and TAB is pressed focus should remain on current element to allow adding more values
            this.element.keydown(function(e) {
                self.menu.active && e.keyCode == $.ui.keyCode.TAB && e.preventDefault();
            });
            $p._create.call(this);
        },
        _initSource: function() {
            // Change the way arrays are handled by making autocomplete think the user sent his own source callback instead of an array
            // The way autocomplete is written doesn't allow me to do it in a prettier way :(
            if ($.isArray(this.options.source)) {
                var array = this.options.source, self = this;
                this.options.source = function(request, response) {
                    response(self.filter(array, request)); // Use our filter() and pass the entire request object so the filter can tell what's currently selected
                };
            }

            // call autocomplete._initSource to create this.source function according to user input
            $p._initSource.call(this);

            // Save a copy of current source() function, than new source() sets request.selected and delegate to original source
            var _source = this.source;
            this.source = function(request, response) {
                request.selected = this.element.val().split(/\s*,\s*/);
                request.selected.pop(); // don't include the term the user is currently writing as selected
                _source(request, response);
            };
            // TODO: instead of overwritting this.source, I can overwrite _search which is easier, but than I'll have to repeat jQuery-UI's code that might change
        },

        // Like $.ui.autocomplete.filter, but excludes items that are already selected
        filter: function(array, request) {
            return $.grep($a.filter(array, request.term), function(value) {
                return $.inArray(value, request.selected) == -1;
            });
        }
    });
})(jQuery, jQuery.ui.autocomplete);

But you should include some jquery auto complete js files from jquery.UI.Download files from there and include in project.

jquery.ui.core

jquery.ui.widget

jquery.ui.position

jquery.ui.autocomplete

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文