Examples of new, create actions for has_many :through associations (nested)

我怕爱的太早我们不能终老 提交于 2019-11-30 06:48:32

问题


I found this code on http://guides.rubyonrails.org/association_basics.html#the-has_one-through-association:

class Document < ActiveRecord::Base
  has_many :sections
  has_many :paragraphs, :through => :sections
end

class Section < ActiveRecord::Base
  belongs_to :document
  has_many :paragraphs
end

class Paragraph < ActiveRecord::Base
  belongs_to :section
end

This is exactly what I'm trying to accomplish, but I'm still very new to Rails and was wondering if someone could show me a sample of the forms and the controllers needed in order to be able to create records for this setup?

I was able to create the first part (document has many sections) but I'm stuck on figuring out how to implement sections have many paragraphs and how to be able to reference between the three. I've searched high and low for the above example and would REALLY appreciate a sample code of the new, create, update actions and corresponding forms.

Thanks so much in advance!

UPDATED: I really appreciate your help with this and thanks for the quick reply. Perhaps I need to clarify a little.

I have my 3 models (user, publication, issue) and they are separated in each of their own views and controllers. The goal is to have a control panel where logged in users can click links to:

a) add/edit/delete publications related to individual users

b) add/edit/delete issues related to individual publications

Hence I have 3 separate forms as well (user, publication & issue).

In my publications_controller I managed to:

@publication = current_user.publications.build(params[:publication])

Which links users and publications together and populates with the correct user_id field in the publications model (which is not listed in attr_accessible) so that works great.

Now my challenge is to add issues to publications and this is where I fall a little short. I have a menu where I can add issues, but I don't want the publication_id field in the form and neither have it in attr_accessible in the model. I want to create an issue through the user with the selected publication.

I'm sorry if I can't explain it well this is all still very new to me, and possibly also why I'm having trouble searching for the correct terminology.


回答1:


Hope this will help you:

Models:

class Document < ActiveRecord::Base
    has_many :sections
    has_many :paragraphs, :through => :sections
    accepts_nested_attributes_for :sections, :allow_destroy => true
    accepts_nested_attributes_for :paragraphs, :allow_destroy => true
end

class Section < ActiveRecord::Base
    belongs_to :document
    has_many :paragraphs
 end

class Paragraph < ActiveRecord::Base
    belongs_to :section
end

Controllers:

class DocumentsController < ApplicationController

    def new
        @document = Document.new
        @document.sections.build
        @document.paragraphs.build
    end
end

Views:

form_for @document do |f|

     ----document fields----------

     f.fields_for :sections do |s|
        ----sections fields----------
     end

     f.fields_for :paragraphs do |s|
          ----paragraphs fields----------
     end

end

Thanks



来源:https://stackoverflow.com/questions/16999292/examples-of-new-create-actions-for-has-many-through-associations-nested

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!