【问题标题】:Wrong number of arguments -trying to implement an autocomplete with jQueryui错误数量的参数 - 尝试使用 jQueryui 实现自动完成
【发布时间】:2015-04-10 16:41:26
【问题描述】:

我正在尝试按照此示例 How to set up jquery ui autocomplete in rails 实现自动完成表单

当我转到 http://localhost:3000/initiatives/10.json?term=Ion 时,我得到一个“错误的参数数量 1 对 0”。

在我的倡议#show 中,我有以下代码:

if params[:term]
  Error points to this line - @people = User.all(:all, :conditions => ['name LIKE ?', "#{params[:term]}%"])
else
  @people = User.all
end

respond_to do |format|  
  format.html  
  format.json { render :json => @people.to_json }
end

我正在使用 PostgreSQL,我认为查询不是针对该数据库进行的。

这是自动完成的脚本:

 <script type="text/javascript">
    $(function() {

    $('#delegatee').autocomplete({  
            minLength: 2,
            source: '<%= json_path %>',

            focus: function(event, ui) {
                console.log("xxxxxx");
                console.log(ui);

                $('#delegatee').val(ui.item.name);
                return false;
            },

            select: function(event, ui) {

                $('#delegatee').val(ui.item.name); 
        $('#delegatee_id').val(ui.item.id);
                return false;
            }
        })
        .data("uiAutocomplete")._renderItem = function( ul, item ) {
            return $( "<li></li>" )      
                .data( "item.autocomplete", item )

                .append( "<a>" + item.name + "</a>" )
                .appendTo( ul );
        };
    });

</script>

"" 导致 /initiatives/:id.json 呈现数据..我认为正确。

当我尝试表单时,它在控制台中给我一个 500 Internal Server 错误,我相信这是我上面描述的错误,它指向我在 jquery 脚本中的这一行:xhr.send( ( options.hasContent &amp;&amp; options.data ) || null );

我正在使用 Rails 4.2.0.beta4 和 Ruby 2.0.0p195。

【问题讨论】:

    标签: jquery-ui ruby-on-rails-4


    【解决方案1】:

    在 Rails > 4.0 中,ActiveRecord's .all 在传递时不会采用多个参数,因此会出现 Wrong number of arguments 1 for 0 错误。

    您尝试设置的查询只是:

    @people = User.where('name LIKE ?', "#{params[:term]}%")
    

    希望对你有帮助!

    【讨论】:

      猜你喜欢
      • 2015-11-30
      • 2011-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多