Ruby on rails 3 jQuery TokenInput插件,具有深度嵌套的_属性,在编辑时预先填充令牌
我的应用程序采用以下结构Ruby on rails 3 jQuery TokenInput插件,具有深度嵌套的_属性,在编辑时预先填充令牌,ruby-on-rails-3,activerecord,nested-attributes,jquery-tokeninput,Ruby On Rails 3,Activerecord,Nested Attributes,Jquery Tokeninput,我的应用程序采用以下结构 class Foo < ActiveRecord::Base has_many :examples, :dependent => :destroy accepts_nested_attributes_for :examples end class Example < ActiveRecord::Base belongs_to :foo has_many :codes, :dependent => :destroy acc
class Foo < ActiveRecord::Base
has_many :examples, :dependent => :destroy
accepts_nested_attributes_for :examples
end
class Example < ActiveRecord::Base
belongs_to :foo
has_many :codes, :dependent => :destroy
accepts_nested_attributes_for :codes, :reject_if => lambda { |a| a[:code].blank? }
end
class Code < ActiveRecord::Base
belongs_to :example
has_many :code_kinds
has_many :kinds, :through => :code_kinds
attr_reader :kind_tokens
def kind_tokens=(ids)
self.kind_ids = ids.split(",")
end
end
class CodeKind < ActiveRecord::Base
belongs_to :code
belongs_to :kind
end
class Kind < ActiveRecord::Base
has_many :code_kinds
has_many :codes, :through => :code_kinds
end
但我不能做@foo.examples.code.kinds.map。。。因为与Foo和examples的关系返回一个集合,所以代码的情况也一样
我只是在用:
那是你的房间里面
现在,如果我没有任何循环,并且所有事情都是通过嵌套的属性和字段来完成的,那么我如何为代码预填充这种类型?
每次你使用
<%= f.text_field :author_tokens, "data-pre" => @book.authors.map(&:attributes).to_json %>
<%
@ctrEx = 0
@ctrCd = 0
%>
<%= form_for @foo ...
<%= f.fields_for :examples do |examples_form| %>
...
<%= examples_form.fields_for :codes do |codes_form| %>
...
<%= codes_form.text_field :kind_tokens, :class => "tag_matcher", "data-pre" => @foo.examples[@ctrEx].codes[@ctrCd].kinds.map(&:attributes).to_json %>
...
<%@ctrCd +=1%>
<%end%>
...
<%
@ctrEx += 1
@ctrCd = 0
%>
<%end%>
<%end%>
@foo.examples[@ctrEx].codes[@ctrCd].kinds.map(&:attributes).to_json