English 中文(简体)
符号输入( 来自不同模式) 上的新表示符名存在真正的困难
原标题:Really having difficulty with New tokens on token input (from different model)

Im 使用jquery-tokeninput, 但有一个叉, 允许 User 为每个 源代码 添加新的自定义符号 ( Tag ) 。

这里的示例( 缩到标签框, 输入几个字母 。 您可以输入不存在的 : < a href=" http:// www.toms guide. fr/ solubiss/ nouveau_ sujet.htm" rel = “ nofollow” > http:// www.toms guide. fr/ solubes/ nouveau_sujet.htm

fork Im 使用 的当前返回值(引用中的新值):

16,42, Subway ,37, McDonald s ,734

我很难在铁路上处理此事。

这是我目前拥有的,而且它不起作用, 原因可能很多,我看不到, 但主要原因是我需要创建新的标签实例, 但没有保存它们, 这样我就可以将它们反馈到象征性输入中, 并在提交表格时保存新的标记和新资源。 如果您使用标记, 新的标记和新资源不会创建身份标识 。

resource.rb

attr_accessor :tokens_list

# CUSTOM TOKENS                 
def tag_tokens=(tokens)
  self.tokens_list = tokens.split(",")

  if new_custom_tokens?
    self.tokens_list.each do |token|
      tokens_list << token if token.include? " "
    end
  end

  self.tag_ids = self.tokens_list
end

def new_custom_tokens?
  self.tokens_list.each do |token|
    return true if token.include? " "
  end
  false
end

resources_controller.rb

def create
  @title = "Submit Resource"
  @resource = Resource.new(params[:resource])

  assign_to_global_user?

  # CUSTOM TOKENS
  if @resource.new_custom_tokens?
    custom_token_time_restriction
    # Create Tag.new
  end

  if @resource.valid?
    @resource.save
    flash[:notice] = "Your link has been successfully submitted."
    redirect_to root_url
  else
    render :action => :new
  end 
end

def assign_to_global_user?
  if user_signed_in?
    @resource.user_id = current_user.id
  else
    @resource.user_id = User.find_by_username("Global_User").id
  end
end

private

# CUSTOM TOKENS   
def custom_token_time_restriction
  limit = 7 # days
  if (@resource.user_id != User.global_user_id) and (Time.now - limit.days > User.find(@resource.user_id).created_at)
    # TODO: Check if they are anonymous or their account is newer than 7 days
  else
    flash[:notice] = "You be Logged in to add new tags, and your account must be older than #{limit} days."
    render :action => :new
  end
end

new.html.erb (for resource#new)

<div class="field">
  <%= f.label :tags %>
  <%= f.text_field :tag_tokens, "data-pre" => @resource.tags.to_json(:only => [:id, :name]), :class => :tagbox %>
</div>
最佳回答

我也有同样的问题,这就是我所做的:

这是我返回 Json 格式的搜索符号的函数 。

tags = TagMaster.where("name LIKE ?", "%#{params[:q]}%").limit(10)
if tags == []
  list << {"id" => "0","name"=>new_tag.rstrip}
else
  tags.each { |tag| list << {"id" => tag.id.to_s, "name" => tag.name }}
end

respond_to do |format|
  format.json { render :json => list.to_json, :layout => false }
end

现在, 这将允许显示您在自动完全下载键中键入的任何内容, 点击它会显示为标记 。

现在您无法添加任何更多的自定义标记, 因为数据库中没有的标记会返回 ID 0, 所以此时只允许一个自定义标记 。

这个问题,我跟着。

var k = jQuery.noConflict();
k("#project_tags").tokenInput("tag_list", {
  hintText: "Enter Tags for your Project",
  noResultsText: "No Such Tags",
  searchingText: "Looking for your Tags",
  preventDuplicates: true,
  theme: "facebook",
  onAdd: function (item) {
    if (item.id ==  0 ) {
      k.ajax({
    url:  /add_project_tag ,
        data: { name: item.name },
    success:function(data) {
          k("#project_tags").tokenInput("add", {id: data, name: item.name});
          k("#project_tags").tokenInput("remove", {id:  0  });
        }
  });
    }
  }
});

您可以在此看到, 我将添加_ project_ tag 调用到数据库中存储该自定义标签, 并返回该插入标签的代号。 所以现在您只需在 s id 中添加相同的标记, 然后用 0 来删除符号 。

所以现在没有0的标记了 你可以随心所欲地添加多少个新的标记。

希望这有帮助 如果再有问题的话 抛出问题来

问题回答

暂无回答




相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签