繁体   English   中英

Rails中的jQuery Tokeninput错误

[英]jQuery Tokeninput Error in Rails

我正在尝试使用Railscast#258(修订版)中所示的jQuery Tokeninput。 当我在tokeninput字段中输入内容时,该字段不会出现结果下拉列表,并且出现以下javascript错误: Uncaught TypeError: Cannot call method 'replace' of undefined

当我对其进行手动查询时,我的json数据工作正常,服务器请求看起来也很好。 我正在尝试在问题表中搜索“内容”列,因此将propertyToSearch设置为“ content”。

这是我的代码:

咖啡脚本:

jQuery ->
$('#fact_issue_tokens').tokenInput "/issues.json"
    theme: 'facebook'
    zindex: 11001
    propertyToSearch: 'content'
    tokenValue: 'content'
    hintText: 'Enter an issue'
    preventDuplicates: true

问题模型:

def self.tokens(query)
  issues = where("content like ?", "%#{query}%")
  if issues.empty?
    [{id: "<<<#{query}>>>", content: "New: \"#{query}\""}]
  else
    issues
  end
end

def self.ids_from_tokens(tokens)
  tokens.gsub!(/<<<(.+?)>>>/) { create!(content: $1).id }
  tokens.split(',')
end

问题负责人:

def index
  @issues = Issue.order(:content)

  respond_to do |format|
    format.html
    format.json { render json: @issues.tokens(params[:q]) }
  end
end

形成:

<%= form_for(Fact.new, :url => kase_facts_path(current_kase), :html => {:class => "form-   
inline"}) do |f| %>

  <%= f.text_field :page, placeholder: 'Page' %>
  <%= f.text_field :description, placeholder: 'Description' %>

  <%= f.label :issue_tokens, 'Issue tags' %>
  <%= f.text_field :issue_tokens %>

  <%= f.hidden_field :source_id, :value => @source.id %>

  <%= f.submit 'Add Fact' %>
<% end %>

@斯科特你试试这个

jQuery ->
  $('#fact_issue_tokens').tokenInput '/issues.json'
    theme: 'facebook'
    tokenLimit: 5
    minChars: 4
    preventDuplicates: true
    searchingText: "Enter an issue..."
    prePopulate: $('#fact_issue_tokens').data('load')

考虑一下索引,因为您正在使用(:content)而不是name可能是您的问题。 我很确定为什么,但是我使用了标题,却遇到了问题,我以为是mysql之类的东西。 当您访问

http://localhost:3000/issues.json

您是否获得JSON数据?

编辑。

您能为您的表格试试吗?

<div class="field">
  <%= f.label :issue_tokens, "Issues" %><br />
  <%= f.text_field :issue_tokens, data: {load: @fact.issues} %>
</div>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM