从头开始标记:查询数据库问题 [英] Tagging from Scratch: the Querying Database issue

查看:92
本文介绍了从头开始标记:查询数据库问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@saverio成功地在标记为从头开始:标签云问题

@saverio was successful on answer this database query question on Tagging from Scratch: the Tag Cloud Issue

现在,我正在尝试将带有jQuery-tokenInput的标签系统连接到 Create ,在 http://railscasts.com/上动态地查找标签Episodes / 258-token-fields-revised

Now I'm trying to connect the tagging system with the jQuery-tokenInput to Create and Find tags dynamically as on http://railscasts.com/episodes/258-token-fields-revised.


  • 我的猜测是这是Posgresql数据库的查询问题。 / li>
  • 我已正确安装了Postgresql

  • jQuery-tokenInput位于 Application.js <$ c $上c> // = require jquery.tokeninput

  • 以某种方式可以从数据库中加载标签,但是无法动态查询与在 pictures.js.coffee 代码中列出。

  • My guess is that is a Query problem to the Posgresql Database.
  • I've got Postgresql correctly installed
  • jQuery-tokenInput is on its place on Application.js //= require jquery.tokeninput
  • Somehow it can load the tags from what was already on the database, but it fails querying the same words dynamically as listed below on pictures.js.coffee code.

遵循所有相关范围:

pictures.js.coffee

pictures.js.coffee

jQuery ->
  $('#picture_tag_tokens').tokenInput '/tags.json'
  theme: 'facebook'
  prePopulate: $('#picture_tag_tokens').data('load')

/ views / pictures / _form

/views/pictures/_form

<div class="field">
  <%= f.label :tag_tokens, "Tags (separated by commas)" %><br />
  <%= f.text_field :tag_tokens, data: {load: @picture.tags} %>
</div>

这里我的逻辑有点迷茫

/models/picture.rb

/models/picture.rb

class Picture < ActiveRecord::Base
  attr_accessible :description, :title, :tag_tokens
  has_many :taggings
  has_many :tags, through: :taggings
  attr_reader :tag_tokens

  #The **below** is the relevant part for the #view/pictures/_form
  def tag_tokens=(tokens)
    self.tag_ids = Tag.ids_from_tokens(tokens)
  end

  def self.tagged_with(name)
    Tag.find_by_name!(name).pictures
  end

  def self.tag_counts
    Tag.select("tags.*, count(taggings.tag_id) as count").
    joins(:taggings).group("tags.id")
  end

  def tag_list
    tags.map(&:name).join(", ")
  end

  def tag_list=(names)
    self.tags = names.split(",").map do |n|
      Tag.where(name: n.strip).first_or_create!
    end
  end
end

下面我能弄清楚我无法查询数据库

Below I could figure out that I'm not being able to query the Database

/models/tag.rb

/models/tag.rb

class Tag < ActiveRecord::Base
  attr_accessible :name
  has_many :taggings
  has_many :pictures, through: :taggings

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

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

我也是如何设置标签控制器行为

And so was how I set my Tags controller behavior

#controllers / tags_controller.rb

#controllers/tags_controller.rb

class TagsController < ApplicationController
  def index
   @tags = Tag.all
   respond_to do |format|
     format.html
     format.json { render json: @tags.tokens(params[:q]) }
   end
  end
end

所以,为什么我不能查询Postgresql却不能创建查找动态吗?

So, Why I can't Query the Postgresql and I'm not able to Create or Find Dynamically?

推荐答案

我加入了一个非常简单的解决方案:使用宝石 ActsAsTaggableOn jQuery Tokeninput

I incorporated a solution that's easy enough: using the Gem ActsAsTaggableOn with jQuery Tokeninput

如果您通过下面的算法,将会发现上面的问题只是对以下配置的误解标记系统和jQuery。以下步骤将对您有所帮助:

If you go through the algorithm below, you will figure out that the question above was just a misconception about the configuration between the Tag System and the jQuery. the following steps will help you in your path:

1将jQuery Tokeninput文件放在正确的位置:

1 Put the jQuery Tokeninput files in the right places:

供应商>资产> javascripts> jquery-tokeninput.js

供应商>资产>样式表> token-input-facebook.css(和另外2个)

2在 apps>中的文件上资产> javascripts> application.js 在// = require_tree之前加上这行。 这可以使JavaScript正常工作

2 on the file in apps > assets > javascripts > application.js wright this line before //= require_tree . this make the javascript work

// =需要jquery.tokeninput

3在 apps>中的文件上资产>样式表> * = require_tree之前的application.css.scss 这使css起作用

* =需要令牌输入facebook

4将 gem'acts-as-taggable-on'放入 gemfile code>并运行捆绑安装,其操作步骤与此处

4 Put the gem 'acts-as-taggable-on' on your gemfile and run bundle install and the very same steps as here.

5在 routes.rb 中创建一条粗体路由以获取标记控制器索引(我们将定义)以启用json属性

5 make a coustom route in routes.rb for a tag controller index (we are about to define) to enable the json property

获取'/tag.json',以:'tag#index',如::pictures_tags

6运行 rails g标签控制器索引,然后在该文件上执行以下操作:

6 run a rails g tag controller index and on this file wright the following:


#tag_controller.rb

def索引



     query = params[:q]
     query = query.chomp(" ").downcase
     @tags = ActsAsTaggableOn::Tag.where("tags.name LIKE '%#{query}%' OR tags.name LIKE '#{query}'")

if @tags.empty?
    ActsAsTaggableOn::Tag.find_or_create_by_name_and_id(id: "#{query}", name: "#{query}")
else 
    respond_to do |format|
        format.html
        format.json { render json: @tags.collect{ |tag| {id: tag.name, name: tag.name } } }
    end
 end            




end

end

7并最终将所有内容与将放置在<$ c上的代码粘合在一起$ c> app>资产> javascript> your_file_with_tags.js.coffee

7 and finally glue everything with the code you will place on app > assets > javascript > your_file_with_tags.js.coffee


#your_file_with_tags.js.coffee

jQuery->



$('#field_with_tag_list').tokenInput '/tag.json'

    theme: 'facebook'

    prePopulate: $('#field_with_tag_list').data('load')

    preventDuplicates: true

    resultsLimit: 3

    tokenLimit: 7

    tokenDelimiter: ","

    searchingText: "hit space for New Tag"



PS:# field_with_tag_list在您生成的视图中(通常是_form部分),用于插补这样的标签:(#controller_tag_list)即#pictures_tag_list



< div class = field>

`<%= f.label :tag_list %><br />`

`<%= f.text_field :tag_list, data: {load: @picture.tag_list.map {|tag| { id: tag, name: tag }}} %>`

< / div>

这篇关于从头开始标记:查询数据库问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆