Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
494 views
in Technique[技术] by (71.8m points)

ruby on rails - Examples of new, create actions for has_many :through associations (nested)

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...