使用 Rails 设置属性jQuery Tokeninput (Railscasts #258)
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论