如题,我的是 Rails 5.2,并调用的时 @ichord 大神的 at.js。 现在,我能获取全部的当前用户,但是我不能通过后台获取用户的一些信息,并显示出来。
相关代码如下:
## users_controller.rb
def mentions
respond_to do |format|
format.json { render :json => Mention.all(params[:q]) }
end
end
## routes.rb
get 'mentions', to: 'users#mentions'
## mentions.rb
class Mention
attr_reader :mentionable
include Rails.application.routes.url_helpers
def self.all(letters)
return Mention.none unless letters.present?
users = User.limit(5).where('name LIKE ?', "#{letters}%").compact
users.map do |user|
{ name: user.name, img: user.picture.nil? ? "images/default_member.png" : user.picture.url, name_before: user.name_before ? user.name_before : "" }
end
end
def self.create_from_text(post)
potential_matches = post.content.scan(/@\w+/i)
potential_matches.uniq.map do |match|
mention = Mention.create_from_match(match)
next unless mention
post.update_attributes(content: mention.markdown_string(post.content))
# You could fire an email to the user here with ActionMailer
mention
end.compact
end
def self.create_from_match(match)
user = User.find_by(name: match.delete('@'))
UserMention.new(user) if user.present?
end
def initialize(mentionable)
@mentionable = mentionable
end
class UserMention < Mention
def markdown_string(text)
host = Rails.env.development? ? 'localhost:3000' : '' # add your app's host here!
text.gsub(/@#{mentionable.name}/i,
"[**@#{mentionable.name}**]("")")
end
end
end
## comments_controller.rb
def new
@comment = Comment.new
Mention.create_from_text(self)
end
## markdown.js
## 获取当前页面的全部用户,并去重。
function logins(){
let uName = [];
$("a.username").each(function () {
uName.push($(this).text());
});
return $.unique(uName);
}
### 调用atwho功能。
function mentionable(logins){
if(logins.length > 0){
$("textarea").atwho({
at: '@',
limit: 8,
callbacks:{
remoteFilter: function (query,callback) {
if(query.length < 1){
return false;
}else{
$.getJSON('/mentions',{q:query},function(data){
console.log(data)
callback(data);
});
}
}
},
'data':'/mentions',
displayTpl: "<li data-value='@${name}'><img src='${img}' height='20' width='20' style='border-radius: 20px'/><span class='mlr5'>${name}</span> <small>${name_before}</small></li>",
});
}else{
console.log("无内容");
}
}
mentionable(logins());
请问下路过的各位大神,我按上述代码,当我在评论框,输入@
时,提示错误信息如下,并且没有用户下拉框,但是在@后,输入字符,可以查询的到用户信息。
Completed 500 Internal Server Error in 9ms (ActiveRecord: 1.1ms)
NoMethodError - undefined method `none' for Mention:Class:
app/models/mention.rb:6:in `all'
app/controllers/users_controller.rb:73:in `block (2 levels) in mentions'
app/controllers/users_controller.rb:72:in `mentions'