使用 at.JS @Mentions 与 Devise [Ruby on Rails 4] 一起使用



根据我在调查此问题并从头开始创建用户时所学到的,而无需设计。是 Devise 不接受[GET]请求,因此我无法使用 JSON 访问用户模型。有没有人实施过包含At.jsDevise的解决方案?如何将用户模型连接到JS component@Mentions symbol

针对在@Seichleon的帮助下所做的更改进行了更新。

atwho-post.coffee

jQuery ->
  $('[data-mentionable]').atwho({
    at: "@",
    data: '<%= mentionables_path %>',
    insertTpl: '${username}',
    displayTpl: '<li data-id="${id}"><span>${username}</span></li>',
    limit: 10,
  });

post _form.html.erb (等待数据属性)

<%= f.text_area :body, maxlength: @maximum_length, id: 'body', data: {behavior: 'autocomplete'} %>
这有

几件事。首先,让我们创建一个操作来处理获取用户。

添加路由:

get 'share/users', as: :mentionables

将实际操作添加到控制器:(在我的情况下,控制器将是共享控制器)

class ShareController < ApplicationController
    def users
        render json: User.all, root: false
    end
end

现在,每当您访问share/users时,您都应该得到这样的东西作为回报:

[  
    {  
       "id":2,
       "email":"test@2build.it",
       "name": "sergio",
       ...
    },
    ...
]

有了这个,您可以像这样设置.js:

$('[data-mentionable]').atwho({
    at: '@',
    data: '<%= mentionables_path %>',
    insertTpl: '${name}',
    displayTpl: '<li data-id="${id}"><span>${name}</span></li>',
});

有了这个,它应该根据用户的名字列出用户。

Post _form.html.erb view

<%= f.text_area :body, maxlength: @maximum_length, id: 'body', data: {behavior: 'autocomplete'} %>

atwho-post.coffee

jQuery ->
  $('[data-behavior="autocomplete"]').atwho({
    at: "@",
    data: "/share/users.json",
    insertTpl: '${username}',
    displayTpl: '<li data-id="${id}"><span>${username}</span></li>',
    limit: 15,
  });

最新更新