如何将pg_search_scope与rails-autocomplete-jquery一起使用?

时间:2015-06-12 07:18:43

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

我正在使用pg_searchrails-jquery-autocomplete

我的搜索范围由protected boolean handleRequestValidationErrors(MessageContext messageContext, SAXParseException[] errors) 提供,使用方式如下:*{ margin:0px; padding:0px; }

我尝试在我的pg_search控制器上使用Node.node_search(query)调用方法node_search,如下所示:

autocomplete

但是我收到错误消息,Node上没有名为autocomplete :node, :node_search 的列。

我也试过说Node,但只查询列node_search。它实际上并没有像我想要的那样将搜索查询传递给我的autocomplete :node, :name, scope: :node_search

基本上,我想要发生的是,每当有人输入1或2个字符,而不是检查我的node.name模型上的Node.node_search列或任何其他列时,我希望它发送给查询name(现在成功接受查询)。

我如何实现这一目标?

修改1

理想情况下,我希望能够在不用JS的情况下做到这一点。如果那不存在,我想我可以使用JS。但我更愿意在实际的rails-jquery-autocomplete gem中使用一些东西。

2 个答案:

答案 0 :(得分:0)

您应该将自动填充发送请求发送到您的node_search路由:

$("your autocomplete").autocomplete({
  source: function (request, response) {
    $.ajax({
        url: "your endpoint url",
        data: { query: request },
        success: function (data) {
            $this.val(data);
        },
        error: function () {
            alert('oops')
        }
    });
});

});'

答案 1 :(得分:0)

您可以尝试:

    import sqlite3
    from contextlib import closing

    # creating a connection
    conn = sqlite3.connect("babynames.db")

    # creating table for boys
    with closing(conn.cursor()) as c :
        query = """CREATE TABLE BOYS(
                    Rank INTEGER PRIMARY KEY,
                    Name TEXT NOT NULL,
                    [Total Babies] INTEGER NOT NULL,
                    [Total Percentage] REAL NOT NULL)
                """
        c.execute(query)

    # # creating table for girls
    with closing(conn.cursor()) as c :
        query = """CREATE TABLE GIRLS(
                    Rank INTEGER PRIMARY KEY,
                    Name TEXT NOT NULL,
                    [Total Babies] INTEGER NOT NULL,
                    [Total Percentage] REAL NOT NULL)
                """
        c.execute(query)

    # reading file contents into two separate tables
    print("Adding data to database\n")
    f = open("babynames.txt", "r")
    for x in f:
        x = x.split("  ") # Splitting each attribute
        # adding new "boy name" to boys table
        with closing(conn.cursor()) as c:
            query = """INSERT INTO BOYS
                        (Rank, Name, [Total Babies], [Total Percentage])
                        Values(?, ?, ?, ?)
                    """
            c.execute(query, (int(x[0]), x[1], int(x[2]), float(x[3])))
            conn.commit()

        # adding new "girl name" to girls table
        with closing(conn.cursor()) as c:
            query = """INSERT INTO GIRLS
                        (Rank, Name, [Total Babies], [Total Percentage])
                        Values(?, ?, ?, ?)
                    """
            c.execute(query, (int(x[0]), x[4], int(x[5]), float(x[6])))
            conn.commit()
    f.close()
    print("All entries have been added to the database\n")

    # question 7: How many babies were born that year?
    num_babies = 0
    num_boys = 0
    num_girls = 0
    with closing(conn.cursor()) as c:
        query = """
                    SELECT sum(b.[Total Babies]) AS TotalBoys, sum(g.[Total Babies]) AS TotalGirls 
                    FROM BOYS b JOIN GIRLS g ON b.RANK=g.RANK
                """
        c.execute(query)
        res = c.fetchall()
        tup = res[0]
        num_boys = tup[0]
        num_girls = tup[1]
        num_babies = num_boys + num_girls

    print("The number of babies born that year is {}\n".format(num_babies))

    # question 8: Are there more boys or girls in that year?
    # Doing directly using results of previous query
    if(num_boys > num_girls):
        print("More boys were born that year\n")
    elif(num_boys < num_girls):
        print("More girls were born that year\n")
    else:
        print("Same no. of boys and girls were born that year\n")

    # question 9
    # Out of the total number of babies born in that year, cumulatively speaking and starting from the
    # first one, what is the name of the boy and girl that crossed 50% of the total baby born in that
    # year?
    with closing(conn.cursor()) as c:
        query = """
                    SELECT b1.Rank, b1.Name, sum(b1.[Total Percentage]) AS CPercent
                    FROM BOYS b1, BOYS b2
                    WHERE b1.[Total Percentage] <= b2.[Total Percentage] OR (b1.[Total Percentage]=b2.[Total Percentage] AND b1.Rank = b2.Rank AND b1.Name = b2.Name)
                    GROUP BY b1.Rank, B1.Name, b1.[Total Percentage]
                    ORDER BY b1.[Total Percentage] DESC, b1.Rank ASC
                """
        c.execute(query)
        cumulative_boys = c.fetchall()

        for boy in cumulative_boys:
            if(boy[2] >= 50):
                 print("The boy who crosed 50%% of the total babies born that year is ",boy[1])
                 break

    # Repeat above for girls, with table name altered
    with closing(conn.cursor()) as c:
        query = """
                    SELECT g1.Rank, g1.Name, sum(g1.[Total Percentage]) AS CPercent
                    FROM GIRLS g1, GIRLS g2
                    WHERE g1.[Total Percentage] <= g2.[Total Percentage] OR (g1.[Total Percentage]=g2.[Total Percentage] AND g1.Rank = g2.Rank AND g1.Name=g2.Name)
                    GROUP BY g1.Rank, g1.Name, g1.[Total Percentage]
                    ORDER BY g1.[Total Percentage] DESC, g1.Rank ASC
                """
        c.execute(query)
        cumulative_girls = c.fetchall()
        for girl in cumulative_girls:
            print(girl)
            if(girl[2] >= 50):
                print("The boy who crosed 50%% of the total babies born that year is ",girl[1])
                break

    # closing connection
    conn.close()

当它接收到一系列作用域并将它们链接起来时。

您必须在模型中定义范围:

autocomplete :node, :node_search, scopes: [:named_scope_1, :named_scope_2, ...]