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

发布于 2024-12-21 02:22:17 字数 1645 浏览 0 评论 0原文

我正在尝试使用 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' });

谢谢!

I am trying to use the jQuery Tokeninput plugin to set attributes for a virtual attribute (using fields_for). I have no problem setting the attribute without the plugin activated, but when Tokeninput is activated it does not pass the attributes of the content. The plugin is otherwise working properly (populating the tokens).

Any ideas why the content is empty when the plugin is active?

Here is my model:

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

Here is the form:

<%= 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>
...

And the JS:

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

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文