ActsAsTaggableOn + jQueryTokenField + Rails 4

时间:2015-02-19 10:05:02

标签: ruby-on-rails-4 acts-as-taggable-on jquery-tokeninput

我尝试按照本文中使用的示例:http://bloginius.com/blog/2013/12/31/how-integrate-acts-as-taggable-on-with-jquery-token-input-with-rails-3/并将其应用于我的场景,但是在Rails 4.2中。如下:

<%= simple_form_for([:admin, @case_law]) do |f| %>
  <%= f.error_notification %>

  <div class="form-inputs">
    <%= f.input :title %>
    <%= f.input :citations %>
    <%= f.input :case_number %> 
    <%= f.input :facts, input_html: {rows: 15} %>
    <%= f.input :holding, input_html: {rows: 15} %>
    <%= f.input :jurisdiction, as: :country, priority: countries %>
    <%= f.input :court, collection: court_listings %>
    <%= f.input :judgment_date, as: :date_picker %>
    <%= f.input :tag_list_tokens, data: {load: @case_law_tags} %>

  </div>

  <div class="form-actions">
    <%= f.button :submit %>
  </div>
<% end %>

case_laws.js.coffee

jQuery ->
    $('#case_law_tag_list_tokens').tokenInput '/admin/case_laws/tags.json',
        theme: 'facebook'
        minChars: 2
        allowCustomEntry: true
        preventDuplicates: true
        prePopulate: $('#case_law_tag_list_tokens').data('load')

LIB /扩展/ tag_extend.rb

 module TagExtend
   extend ActiveSupport::Concern

    included do
        scope :by_tag_name, -> name { where("name like ?", "%#{name}%") }

        def self.token_input_tags
          where(nil).map{ |t| {id: t.name, name: t.name} }
         end
       end
     end

/initializers/acts_as_taggable_on.rb

ActsAsTaggableOn::Tag.send(:include, Intejus::TagExtend)

控制器/管理/ case_laws_controller.rb

class Admin::CaseLawsController < AdminController
  before_action :find_tags, only: [:new, :create, :edit, :update]
  ....
    def tags
      tags = ActsAsTaggableOn::Tag.by_tag_name(params[:q]).token_input_tags
      respond_to do |format|
       format.json { render json: tags }
      end
    end
  private
   def find_tags
      @case_law_tags = params[:id].present? ?    CaseLaw.find(params[:id]).tags.token_input_tags : []
   end

这是生成的html:         &lt; \ div class =“form-group string optional case_law_tag_list_tokens”&gt;&lt; \ label class =“string optional control-label”for =“case_law_tag_list_tokens”&gt;标记列表标记&lt; \ ul class =“token-input-list -facebook“&gt;&lt; \ li class =”token-input-input-token-facebook“&gt;&lt; \ input type =”text“autocomplete =”off“autocapitalize =”off“id =”token-input- case_law_tag_list_tokens“style =”outline:none; width:209.993048667908px;“&gt;&lt; \ tester style =”position:absolute; top:-9999px; left:-9999px; width:auto; font-size:11.9999990463257px; font -family:Verdana,sans-serif; font-weight:400; letter-spacing:0px; white-space:nowrap;“&gt;&lt; \ input class =”string optional form-control“type =”text“name = “case_law [tag_list_tokens]”id =“case_law_tag_list_tokens”style =“display:none;”&gt;

我的第一期: 如果单击编辑页面链接,则不会加载标记字段,并显示常规文本字段。如果我刷新编辑页面,则会出现令牌字段。

第二期: 自动完成功能正常,标签已创建,但令牌字段不会预先填充。

1 个答案:

答案 0 :(得分:1)

我意识到我应该使用文本字段:        <%= f.text_field :tag_list_tokens, data: {load: @case_law_tags} %> 而不是simple_form帮助器 <%= f.input :tag_list_tokens, data: {load @case_law_tags} %>

重装的问题与turbolinks有关。在链接后,Rails不会完全重新加载文档。它通过ajax加载页面之间的差异。我通过将代码修改为:

来修复此问题

<强> case_laws.js.coffee

tokenizeCaseLawTags = ->
  $('#case_law_tag_list_tokens').tokenInput '/admin/case_laws/tags.json',
    theme: 'facebook'
    minChars: 2
    allowCustomEntry: true
    preventDuplicates: true
    prePopulate: $('#case_law_tag_list_tokens').data('load')

$(document).on "ready page:load", ->
  tokenizeCaseLawTags()