使用Rails + jQuery Tokeninput设置属性(Railscasts#258)

时间:2011-12-12 19:48:05

标签: jquery ruby-on-rails ruby-on-rails-3 jquery-plugins

我正在尝试使用jQuery Tokeninput插件来设置虚拟属性的属性(使用fields_for)。 在没有激活插件的情况下设置属性没有问题,但是当Tokeninput被激活时,它不会传递内容的属性。该插件正常工作(填充令牌)。

插件处于活动状态时内容为空的任何想法?

这是我的模特:

class Fact
 include Mongoid::Document
 field :description, :type => String
 belongs_to :matter
 has_and_belongs_to_many :issues
 attr_reader :issue_contents
 attr_writer :issue_contents
 attr_writer :issue_matter_id
 after_save :assign_issues
 accepts_nested_attributes_for :issues

 def issue_contents
  @issue_contents ||= issues.map(&:content).join(', ')
 end

private

def assign_issues
 @issue_contents ||= issues.map(&:content).join(', ')
  if @issue.blank?
   @issue  = issues[0]
   @issue.destroy
end

if @issue_contents
  self.issues = @issue_contents.split(",").map do |content|
    matter.issues.find_or_create_by(content: (content))          
   end  
  end

以下是表格:

<%= form_for(@fact, :url => matter_facts_path(@fact.matter)) do |f| %>
...
<div class="clearfix">
 <%= f.label :add_fact %>
 <div class="input"><%= f.text_area(:description, :size=>"20x3") %></div>
</div>
<%= f.fields_for :issues do |x| %>
<div class="clearfix">
 <%= x.label :content %>
 <div class="input"><%= x.text_field :content, :id => "issue_content" %></div>
...

和JS:

$(function () { 
 var pathname = window.location.pathname; 
 $('#issue_content').tokenInput(pathname + "/issues.json", 
{ crossDomain: false, propertyToSearch: "content", theme: 'facebook' });

谢谢!

0 个答案:

没有答案