2011-06-08 24 views
2

更新:根据堆栈跟踪,发布在此问题的最后,我认为真正的问题是要弄清楚attr_accessible和构建关联设置我需要让贡献者和链接器属性用新的isbn id进行更新。请帮帮我! ISBN和贡献者 - - 其中有一个多对多的关系nested_form,collection_select,accepting_attributes_for和fields_for,并将记录添加到连接表

this question...

我有两个模型之间的连接表继。我想在使用fields_for的嵌套窗体内的collection_select下拉列表中使用贡献者模型中的现有值。

因此,当我创建新的isbn时,我不想创建新的贡献者记录 - 我希望能够选择现有的记录。我希望能够选择许多贡献者,这就是为什么我使用nested_form gem这使我可以为贡献者动态创建额外的表单域。

我的问题:我应该在连接表上调用方法吗?如果是这样,为什么我得到一个 '丢失块' 的错误,尽管有几乎一切attr_accessible和accepts_nested_attributes_for:

isbn.rb:

attr_accessible :linkers_attributes, :contributors_attributes, :contributor_id, 
    has_many :linkers 
    has_many :contributors, :through => :linkers 
    accepts_nested_attributes_for :contributors 
    accepts_nested_attributes_for :linkers 

contributor.rb:

attr_accessible :linkers_attributes, :isbns_attributes 
    has_many :linkers 
    has_many :isbns, :through => :linkers 
    accepts_nested_attributes_for :isbns 
    accepts_nested_attributes_for :linkers 

连接.RB:

belongs_to :isbn 
    belongs_to :contributor 
    accepts_nested_attributes_for :contributor 
    accepts_nested_attributes_for :isbn 
    attr_accessible :isbn_id, :contributor_id, :isbns_attributes, :contributors_attributes, :contributor_attributes, :isbn_attributes 

的ISBN控制器:

def new 
    @isbn = Isbn.new 
    @title = "Create new ISBN" 
    1.times {@isbn.linkers.build} 
    end 

new.html.erb:

<td class="main"> 
<%= nested_form_for @isbn, :validate => false do |f| %> 
<h1>Create new ISBN</h1> 
<%= render 'shared/error_messages', :object => f.object %> 
<%= render 'fields', :f => f %> 
    <div class="actions"> 
    <%= f.submit "Create" %> 
    </div> 

_fields:(选项1)

<%= f.fields_for :contributors do |contributor_form| %> 
<li> 
<%= contributor_form.label 'Contributor Name' %> 
<%= contributor_form.collection_select(:isbn_id, Contributor.all, :id, :personnameinverted) %> 
</li> 
<%= contributor_form.link_to_remove "[-] Remove this contributor"%> 
<% end %> 
<%= f.link_to_add "[+] Add a contributor", :contributors %> 

这将创建一个新的书号,并在接头用正确的isbn_id的新纪录,但也是贡献者中的新记录,当我真的希望新的链接器记录包含现有的contributor_id时。所以,我想我会做到这一点:

_fields(选项2)

<%= field_set_tag 'Contributor' do %> 
<li> 
<%= f.label 'Contributor Sequence Number' %> 
<%= f.text_field :descriptivedetail_contributor_sequencenumber%> 
<%= tooltip(:xxx, :hover) %> 
</li> 

<%= f.fields_for :linkers do |contributor_form| %> 
<li> 
<%= contributor_form.label 'Contributor Name' %> 
<%= contributor_form.collection_select(:isbn_id, Contributor.all, :id, :personnameinverted) %> 
</li> 
<%= contributor_form.link_to_remove "[-] Remove this contributor"%> 
<% end %> 
<%= f.link_to_add "[+] Add a contributor", :contributors %> 

但这返回一个丢失块错误。

巨大的感谢提前,我花了两个坚实的日子,并认为我可能会弹出。

更新:这里有一个线索,从这个新的代码的堆栈跟踪,因为我想你不能真正称之为基于连接表的方法:

<%= f.fields_for :contributors do |contributor_form| %> 
<li> 
<%= contributor_form.collection_select(:id, Contributor.all, :id, :personnameinverted, :include_blank => true) %> 



Started POST "/isbns" for 127.0.0.1 at 2011-06-08 20:12:48 +0100 
    Processing by IsbnsController#create as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"u0TszLESOnZlr4iEiBlVg6w9B5T+EH0dxJg/0E6PKuQ=", "isbn"=>{"descriptivedetail_titledetail_titleelement_titleprefix"=>"", "descriptivedetail_titledetail_titleelement_titlewithoutprefix"=>"qqq", "istc_id"=>"471", "descriptivedetail_contributor_sequencenumber"=>"", "contributors_attributes"=>{"0"=>{"id"=>"1", "_destroy"=>"false"}, "1307560328932"=>{"id"=>"14", "_destroy"=>"false"}}, "descriptivedetail_contributor_contributorrole"=>""}, "commit"=>"Create"} 
    User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 2 LIMIT 1 
    Contributor Load (0.3ms) SELECT "contributors".* FROM "contributors" INNER JOIN "linkers" ON "contributors".id = "linkers".contributor_id WHERE "contributors"."id" IN (1, 14) AND (("linkers".isbn_id = NULL)) 
Completed in 308ms 

所以它看起来像贡献者标识实际上是被设置,但链接器表没有被传递isbn id。起...

回答

3

好吧。这是我做到的。

安装了茧宝石。 终于轮到安装Haml了,

我把新的和更新的动作分开了,所以新形式只是一个快速获得id集,只有最少的字段。然后用户可以编辑它,并添加贡献者。从用户的角度来看,实际上并不糟糕,因为他们可以快速创建一个isbn,而无需填写一个bazillion字段。

的ISBN/new.html.haml

%td.main 
= semantic_form_for @isbn do |f| 
    %h1 Create new ISBN 
    = render 'shared/error_messages', :object => f.object 
    = render 'fields', :f => f 
    %div#actions 
    = f.submit "Create" 

的ISBN/_fields.html.haml

- f.inputs do 
    = f.input :istc_id, :as => :select, :collection => Istc.all 
    = f.input :descriptivedetail_titledetail_titleelement_titlewithoutprefix 
    %h3 Contributors 
    #tasks 
    = f.semantic_fields_for :linkers do |linker| 
     = render 'linker_fields', :f => linker 
    .links 
     = link_to_add_association 'add contributor', f, :linkers 
    -f.buttons do 
    = f.submit 'Save' 

的ISBN/_linker_fields.html.haml

.nested-fields 
    = f.inputs do 
    = f.input :contributor_id, :label_method => :keynames, :as => :select, :collection => Contributor.all 
    = link_to_remove_association "remove contributor", f 

Isbn.rb

has_many :linkers 
    has_many :contributors, :through => :linkers 
    accepts_nested_attributes_for :contributors, :allow_destroy => true 
    accepts_nested_attributes_for :linkers, :allow_destroy => true 

contributor.rb

attr_accessible :linkers_attributes, :isbns_attributes 
    has_many :isbns, :through => :linkers 
    has_many :linkers 
    accepts_nested_attributes_for :linkers 

linker.rb

belongs_to :contributor 
belongs_to :isbn 
accepts_nested_attributes_for :contributor 

结果

Started POST "/isbns/58" for 127.0.0.1 at 2011-06-09 12:34:14 +0100 
    Processing by IsbnsController#update as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"u0TszLESOnZlr4iEiBlVg6w9B5T+EH0dxJg/0E6PKuQ=", "isbn"=>{"istc_id"=>"360", "descriptivedetail_titledetail_titleelement_titlewithoutprefix"=>"thurs title", "linkers_attributes"=>{"0"=>{"contributor_id"=>"3", "_destroy"=>"", "id"=>"68"}, "1"=>{"contributor_id"=>"71", "_destroy"=>"", "id"=>"69"}, "2"=>{"contributor_id"=>"72", "_destroy"=>"", "id"=>"70"}, "3"=>{"contributor_id"=>"3", "_destroy"=>"", "id"=>"71"}}}, "commit"=>"Save", "id"=>"58"} 
    User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 2 LIMIT 1 
    Isbn Load (8.2ms) SELECT "isbns".* FROM "isbns" WHERE "isbns"."id" = 58 ORDER BY descriptivedetail_titledetail_titleelement_titlewithoutprefix LIMIT 1 
    Linker Load (0.3ms) SELECT "linkers".* FROM "linkers" WHERE "linkers"."id" IN (68, 69, 70, 71) AND ("linkers".isbn_id = 58) 
    AREL (0.4ms) UPDATE "linkers" SET "contributor_id" = 3, "updated_at" = '2011-06-09 11:34:14.509943' WHERE "linkers"."id" = 71 
    Contributor Load (158.3ms) SELECT "contributors".* FROM "contributors" 
Redirected to http://localhost:3000/isbns/58 
Completed 302 Found in 545ms 

换句话说,来自已经动态产生嵌套fields_for,使用formtastic ISBN的编辑形式,我可以使用下拉菜单选择与isbns具有多对多关系的现有撰稿人。

Phew。

相关问题