如何在邮件系统中以用户键入的身份运行搜索

How to run search as user types in messaging system

本文关键字:身份 运行 搜索 用户 邮件系统      更新时间:2023-09-26

我正在开发一个消息传递系统,除了这部分之外,几乎所有事情都完成了。

这是我的表格:

<%= form_for :message, url: messages_path(7) do |f| %>
    <%= f.text_area :message, class: 'form-control', placeholder: 'Write a message...' %>
    <div class="clearfix"><%= f.submit 'Send', class: 'btn btn-primary pull-right' %></div>
<% end %>

如您所见,此表单将向用户发送一条消息,该消息的用户 ID 为 7。因此,创建消息也可以工作,但有些我应该如何以用户类型搜索数据库中的用户,并且在用户选择后,我应该使用用户选择的 id 动态更改此处的值。 messages_path(7) 这就是Facebook或Gmail的方式。我想没有其他办法。我不知道该怎么做。

正如我所说,一切都完成了,我只需要通过用户名作为发件人类型收件人名称运行搜索,以及用户选择的收件人,我必须动态替换id。

谢谢。

我认为您可以使用AJAX从数据库中获取用户。您可以轻松修改要在客户端发布到的ID

我写了一些东西,可能会让你走上正确的轨道:)

function getUsers () {
    $.getJSON('localhost/{your-file-to-user-query}',{
    headers: {
        'Accept': '*/*',
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
        },
    tagmode: "any",
    format: "json",
    async: false
    }).success(function(data) {
        var id, name, html;
        data = JSON.parse(data);
        if(data !== "")
        {
            //Parse all user data and put them in a list
            for(var i = 0; i < data.length; i++){
                id    = data[i].id;
                name  = data[i].name;
                html += '<div onclick = "selectUser(' + id + ')" class = "user-block">'+
                        '<p>' + name + '</p>'+
                        '</div>';
            }
        $('{Some_container_to_put_this_list_in}').html(html);
         }
    }).error(function(data) {
        console.log(data);
    });
}
function selectUser (id) {
    //Don't know alot about RoR but you need to add an id to your form so you can select it easily
    //And than you replace the current id with the id that came from the list item you clicked on 
    $('form').attr('url',messages_path(id))
}