Rails: How can I put the sections together to form a book automatically?

70 Views Asked by At

I'm a freshman to learn Rails and working on my first project about "online book writing".

I've already made the MVC of user,book and section. Association like this:

class User < ActiveRecord::Base
  has_many :sections , dependent: :destroy
end

class Section < ActiveRecord::Base
  belongs_to :book
  belongs_to :user
end

class Book < ActiveRecord::Base
  has_many :sections
end

So I made a relationship between user and section,but no relationship between user and book. for example,user.1 write section.1 of book.1 ,the user.2 write section.2 of book.1.

Then I want to use all the sections of book.1 which written by different users,to form a book.1 automatically. (connect the text field directly in one text field,book_content is sum of section_content)

What should I do in the views or models?

1

There are 1 best solutions below

0
On

You'll have to add an index field to manage the order of the sections. But for now you can do this:

<%= render book.sections %>

And then you'll need to define a partial in sections/_section.html.(erb.haml.whatever)