Creating two resources at once with a form in Rails
Say you have a Post model that has a :title
, :author
, :content
, etc. It also has a :thread_id
attribute, and it belongs_to :thread
.
The thread model doesn't have any custom fields. The only ones you'll be needing are those created by default such as :id
, :cre开发者_JAVA百科ated_at
, and :updated_at
. Threads have_many :posts
.
How do you create a form for a Thread that simultaneously creates both a Thread and a Post?
Related questions, possibly:
- Should I say
<% form_for @new_thread ... %>
or<% form_for @new_post ... %>
? - Should the form be in the Thread view code or the Post view code?
Thanks for your help, I am new to Rails and I'm still learning how to organize my code in it.
check out
http://railscasts.com/episodes/196-nested-model-form-part-1
and the accepts_nested_attributes_for method.
精彩评论